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







Struts And Tiles

Overview


  • Taking care of the website look and feel
  • Tiles is used to create reusable presentation components

Configuration

In order to load the tiles configuration,we need to add the following <plug-in> definition
to the struts-config.xml file.

<plug-in className="org.apache.struts.tiles.TilesPlugin" >
 <set-property property="definitions-config" value="/WEBINF/
tiles-defs.xml" />
 <set-property property="moduleAware" value="true" />
</plug-in>

Two way of  Specifying Tiles definition and their attributes
  • JSP Tile Definition
  • XML Tile Definition.
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"
  • The above taglib is used in the jsp which uses tiles definition.
Tiles definitions

tiles-defs.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software
 Foundation//DTD Tiles Configuration 1.0//EN"
 "http://tiles.apache.org/dtds/tiles-config_1_0.dtd"
>
<tiles-definitions>
  <definition name="mymasterlayout" path="/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="WEB-INF/struts-tiles.tld" 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:insert Attribute name="header"/>
                </td>
            </tr>
            <tr>
                <td width="30%" bgcolor="lightblue">
                    <tiles:insert Attribute name="menu"/>
                </td>
                <td width="70%" bgcolor="lightgreen">
                    <tiles:insert Attribute name="body"/>
                </td>
            </tr>
            <tr height="50">
                <td height="30" colspan="2" bgcolor="lightyellow">
                    <tiles:insert Attribute 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

Plug-in adds a new struts-config.xml
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
 <set-property property="definitions-config" value="/WEB-INF/
tiles-defs.xml" />
 <set-property property="moduleAware" value="true" />
</plug-in>

Example:1

index.jsp
<%taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<body>
  <h3>Struts Tiles Simple Example</h3>


  <html:link page="/example.jsp">Tiles Example</html:link>



</body>
</html:html>

example.jsp
<%page language="java" %>
<%taglib uri="/tags/struts-tiles" prefix="tiles" %>

<tiles:insert page="/template.jsp" flush="true">
   <tiles:put name="title" type="string" value="Welcome" />
   <tiles:put name="header" value="/top.jsp" />
   <tiles:put name="menu" value="/left.jsp" />
   <tiles:put name="body" value="/content.jsp" />
   <tiles:put name="bottom" value="/bottom.jsp" /> 
</tiles:insert>

content.sjp
<p align="left"><font color="#000080" size="5">
Welcome to the Title Tutorial</font></p>

<p align="left"><font color="#000080" size="5">
This is the content page</font></p> 

top.jsp
<font color=red size=25>Top Side</font>


bottom.jsp

<font color=red size=25>Bottom Side</font>


left.jsp

<font color=red size=25>Left Side</font>


template.jsp
<%page language="java" %>
<%taglib uri="/tags/struts-tiles" prefix="tiles" %>
<html>

<head>
    <title><tiles:getAsString name="title" ignore="true"/></title>
</head>

<body>

<table border="1" cellpadding="0" cellspacing="0"
 
width="100%" height="100%" bordercolor="#000000"
 
bgcolor="#E7FDFE">
<tr>
<td width="100%" colspan="2" valign="top"><center>
<tiles:insert attribute=
"header"/></td>
</tr>
<tr>
<td width="30%" ><tiles:insert attribute="menu"/></td>
<td width="77%" valign="top" valign="top">
<tiles:insert attribute=
"body"/></td>
</tr>
<tr>
<td width="100%" colspan="2" valign="top"><center>
<tiles:insert attribute=
"bottom"/></td>
</tr>
</table>

</body>

</html>

Output:





Download Zip Code


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="WEB-INF/struts-tiles.tld" 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:insert Attribute name="header"/>
                </td>
            </tr>
            <tr>
                <td width="30%" bgcolor="lightblue">
                    <tiles:insert Attribute name="menu"/>
                </td>
                <td width="70%" bgcolor="lightgreen">
                    <tiles:insert Attribute name="body"/>
                </td>
            </tr>
            <tr height="50">
                <td height="30" colspan="2" bgcolor="lightyellow">
                    <tiles:insert Attribute name="footer"/>
                </td>
            </tr>
        </table>
    </body>
</html>

tiles-defs.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="ISO-8859-1" ?>

 <!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">

<tiles-definitions>
  <definition name="mymasterlayout" path="/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:-
  • When the user click on login link only body part will be changed as shown below.





  • Again when the user give valid user name and password and click on Login button
    only body region will be reflected and valid access will be shown as given below.



  • Again when the user give Invalid user name and password and click on Login button
    only body region will be reflected and Invalid access will be shown as given below.


Download Eclipse Code without lib

Download code with lib(WAR)