JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Tiles And Struts2

Overview


  • Taking care of the website look and feel
  • Tiles are implemented as a new result type.
  • Each action result that wishes to use a Tiles layout needs to provide
    the attribute “type” with value “tiles”.
  • The Tile name needs to be defined in the
    configuration file “tiles.xml”.

Configuration

In order to load the tiles configuration, a servlet listener needs to
be configured:

<listener>
    <listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
  </listener>

The listener loads the “tiles.xml” configuration file, which defines each tile for your application, from the “WEB-INF” directory.

Tiles definitions

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software
 Foundation//DTD Tiles Configuration 2.0//EN"
 "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"
>
<tiles-definitions>
  <definition name="mymasterlayout" template="/MasterLayout.jsp">
        <put-attribute name="header" value="/Header.html"/>
        <put-attribute name="footer" value="/Footer.html"/>
        <put-attribute name="body" value="/Body.html"/>
        <put-attribute name="menu" value="/LinksPage.html"/>
    </definition>
</tiles-definitions>

The name property of the put-attribute tag defines a tile region.

<%taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
      <table border="1" width="100%" height="100%">
          <tr height="50">
              <td colspan="2" bgcolor="lightyellow">
                  <tiles:insertAttribute name="header"/>
                </td>
            </tr>
            <tr>
                <td width="30%" bgcolor="lightblue">
                    <tiles:insertAttribute name="menu"/>
                </td>
                <td width="70%" bgcolor="lightgreen">
                    <tiles:insertAttribute name="body"/>
                </td>
            </tr>
            <tr height="50">
                <td height="30" colspan="2" bgcolor="lightyellow">
                    <tiles:insertAttribute name="footer"/>
                </td>
            </tr>
        </table>
    </body>
</html>

This is the JSP for one of our overall layouts. You can see several places where we’ve
included the tiles:insertAttribute tags

Required additions to web.xml file
  • Tiles definitions files
  • Tiles listener
<context-param>
    <param-name>org.apache.tiles.impl.
BasicTilesContainer.DEFINITIONS_CONFIG</param-name>

    <param-value>/WEB-INF/tiles.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.apache.tiles.
web.startup.TilesListener</listener-
class>
  </listener>

Plug-in adds a new result type
result-types>
      <result-type name="tiles" class="org.apache.struts2.
views.tiles.TilesResult" 
default="true"/>
    </result-types>

Example:-

This example is a simpe login based  where we taking as a input user name and password and matches the same statically with 'javed' and '123456'....!!!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        Header.html
      </h2>
    </body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        Footer.html
      </h2>
    </body>
</html>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        Body.html
      </h2>
    </body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        Menu.html
      </h2>
    </body>
</html>

HomePage.jsp
<%taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<tiles:insertDefinition name="mymasterlayout"/>

LinkPage.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        <a href="loginform">Login</a><br/>
      
      </h2>
    </body>
</html>

LoginForm.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        <form action="login" method="post">
          Username<input type="text" name="username"/><br/>
          Password<input type="password" name="password"/><br/>
          <input type="submit" value="Login"/>
        </form>
      </h2>
    </body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        <font color="green">
          Valid Access.....
        </font>
      </h2>
    </body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
 4.01 Transitional//EN"
>
<html>
  <body>
      <h2>
        <font color="red">
          Invalid Access.....
        </font>
      </h2>
    </body>
</html>

<%include file="./LoginForm.html" %> 

Masterlayout.jsp
<%taglib uri="http://tiles.apache.org/tags-tiles"
 
prefix="tiles" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
      <table border="1" width="100%" height="100%">
          <tr height="50">
              <td colspan="2" bgcolor="lightyellow">
                  <tiles:insertAttribute name="header"/>
                </td>
            </tr>
            <tr>
                <td width="30%" bgcolor="lightblue">
                    <tiles:insertAttribute name="menu"/>
                </td>
                <td width="70%" bgcolor="lightgreen">
                    <tiles:insertAttribute name="body"/>
                </td>
            </tr>
            <tr height="50">
                <td height="30" colspan="2" bgcolor="lightyellow">
                    <tiles:insertAttribute name="footer"/>
                </td>
            </tr>
        </table>
    </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
  <context-param>
    <param-name>org.apache.tiles.impl.
BasicTilesContainer.DEFINITIONS_CONFIG</param-name>

    <param-value>/WEB-INF/tiles.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.apache.tiles.web.
startup.TilesListener</listener-
class>
  </listener>

    <filter>
      <filter-name>Struts2Filter</filter-name>
      <filter-class>org.apache.struts2.dispatcher
.FilterDispatcher</filter-
class>
    </filter>
    <filter-mapping>
      <filter-name>Struts2Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  
    <welcome-file-list>
      <welcome-file>HomePage.jsp</welcome-file>
    </welcome-file-list>
</web-app>


tiles.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software
 Foundation//DTD Tiles Configuration 2.0//EN" "http://
tiles.apache.org/dtds/tiles-config_2_0.dtd"
>
<tiles-definitions>
  <definition name="mymasterlayout" template="/MasterLayout.jsp">
        <put-attribute name="header" value="/Header.html"/>
        <put-attribute name="footer" value="/Footer.html"/>
        <put-attribute name="body" value="/Body.html"/>
        <put-attribute name="menu" value="/LinksPage.html"/>
    </definition>
  
    <definition name="loginformlayout" extends="mymasterlayout">
      <put-attribute name="menu" value="/Menu.html"/>
        <put-attribute name="body" value="/LoginForm.html"/>      
    </definition>
  
    
    <definition name="loginvalidlayout" extends="mymasterlayout">
      <put-attribute name="menu" value="/Menu.html"/>
        <put-attribute name="body" value="/LoginSuccess.jsp"/>      
    </definition>
    
    <definition name="logininvalidlayout" extends="mymasterlayout">
      <put-attribute name="menu" value="/Menu.html"/>
        <put-attribute name="body" value="/LoginFailure.jsp"/>      
    </definition>
</tiles-definitions>

EmployeeLoginAction.java
package myaction;

import java.util.ArrayList;

public class EmployeeLoginAction {
  private String username;
  private String password;

  public void setUsername(String username) {
    this.username = username;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getUsername() {
    return username;
  }

  public String getPassword() {
    return password;
  }

  public String execute() {
    String key = "";

    if (getUsername().equals("javed")
&& getPassword().equals("123456"))
      key = "valid";
    else
      key = "invalid";

    return key;
  }
}


struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software
 Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd"
>
<struts>
  <package name="default" extends="struts-default">
    <result-types>
      <result-type name="tiles"
        class="org.apache.struts2.views.tiles
.TilesResult" 
default="true" />
    </result-types>

    <action name="loginform">
      <result type="tiles">loginformlayout</result>
    </action>


    <action name="login" class="myaction.EmployeeLoginAction">
      <result name="valid" type="tiles">loginvalidlayout</result>
      <result name="invalid" type="tiles">logininvalidlayout</result>
    </action>
  </package>
</struts>


Output:-












Download Source without lib
Download War with lib