Tiles in Struts2

The Apache Tiles is templating framework is used to design several pages in easiest way. It provides features to divide entire page in different sections including title and page can be inserted in the sections at run time. It is used for same look and feel of several pages. It is only use in Web Application. The tiles frameworks plug-in are available in other framework also like; Spring. It can also be use without any framework in J2EE web application.

Lets demostrate how to integrate Tiles framework with Struts2.
The following example demostractes how to integrate Tiles in Struts 2 using the struts 2 tiles plugin.

Follow the following steps to create this project :
Step 1: Create a Dynamic Web Project in eclipse - "Struts2Tiles".

Step 2: Edit deployment descriptor web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2Tiles</display-name>
<listener>
  <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Step 3: Copy required Struts2 jar files in WEB-INFlib folder in your project :
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.1.2.jar
struts2-tiles-plugin-2.3.1.2.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
xwork-core-2.3.1.2.jar

Step 4: Create a layout page layout.jsp :
<%taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<title><tiles:getAsString name="title"/></title>
</head>
<body topmargin="0">
<table width="500" cellspacing="0" align="center" border="1">
<tr>
   <td colspan="2" height="70">
       <tiles:insertAttribute name="header"/>
   </td>
</tr>
<tr>
   <td width="200" valign="top">
  <tiles:insertAttribute name="menu"/>
   </td>
   <td width="600" valign="top">
      <tiles:insertAttribute name="body"/>
   </td>
</tr>
<tr>
   <td colspan="2" height="30">
     <tiles:insertAttribute name="footer"/>
   </td>
  </tr>
</table> 
</body>
</html>
/td>

Step 5: Create tiles definitiona file tiles.xml :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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="login.def" template="/layout.jsp">    
<put-attribute name="title"  value="This is login Page" type="string"/>
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="menu" value="/mainmenu.jsp"/>
<put-attribute name="body"   value="/loginform.jsp"/>
<put-attribute name="footer" value="/footer.jsp"/>
</definition>    

<definition name="manager.welcome.def" template="/layout.jsp">    
<put-attribute name="title"  value="Welcome Manger" type="string"/>
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="menu" value="/manager_menu.jsp"/>
<put-attribute name="body"   value="/body.jsp"/>
<put-attribute name="footer" value="/footer.jsp"/>
</definition> 

<definition name="clerk.welcome.def" extends="manager.welcome.def">    
<put-attribute name="title"  value="Welcome Clerk" type="string"/>
<put-attribute name="menu" value="/clerk_menu.jsp"/>
</definition>   

<definition name="manager.def" extends="manager.welcome.def">    
<put-attribute name="title"  value="Manager Action" type="string"/>
<put-attribute name="body"   value="/actionbody.jsp"/>
</definition>   

<definition name="clerk.def" extends="manager.def">    
<put-attribute name="title"  value="Clerk Action" type="string"/>
<put-attribute name="menu" value="/clerk_menu.jsp"/>
</definition> 

</tiles-definitions>

Step 6: Create home page index.jsp
<%taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
"/layout.jsp">
  "title"  value="Tiles integration with Struts" type="string"/>
  "header" value="/header.jsp" />
  "menu" value="/mainmenu.jsp"/>
  "body"   value="/mainbody.jsp" />
  "footer" value="/footer.jsp" />

Step 7: Create page for hear section header.jsp
<%taglib prefix="s" uri="/struts-tags" %>
<table width="100%" height="100%" bgcolor="#A1EBF0">
<tr>
<td><s:date name="new java.util.Date()" format="dd MMMM yyyy"/></td>
<td align="center">
<h2>Welcome to Struts2 tutorial on http://techmyguru.com</h2></td></tr>
</table>

Step 8: Create page for footer section footer.jsp
<p align="center"><b><i>Copyright message goes here 2013 </i></b></p>

Step 9: Create default page for body scetion mainbody.jsp
<table  width="100%" align="center" cellpadding="10">
<tr>
<td>
<h2 align="center" style="font-family:Book Antiqua">A Tiles Implementaion</h2>
<p align="justify">
Welcome to the Struts Tiles component.Tiles is a templating system.
 (Include on steriods.It can be used to create a common look and feel 
 for a web application. Tiles can also be used to create reusable view 
 components. This site will help you get started with Tiles. <br>
Tiles can also be used without Struts. A new release of Tiles, Tiles 2,
 is being prepared. If you would like to use Tiles without Struts, see the 
 new Apache Tiles project. The focus of this site is using Tiles with Struts. 
</p>
</td></tr>
</table>

Step 10: Create page for menu section mainmenu.jsp
<table width="100%" align="left" border="0">
<tr>
  <td><a href="index.jsp">Home</a>
  </td>
</tr>
<tr>
  <td><a href="login.jsp">Login</a>
  </td>
</tr>
</table>

Step 11: Create login.jsp
<%taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="login.def"/>

Step 12: Create page for user login loginform.jsp
<%taglib prefix="s" uri="/struts-tags" %>
<table width="100%" align="center" cellpadding="10">
<tr>
<td>
<s:actionerror/>
<s:form action="login" method="post">
  <s:textfield name="loginid" key="app.loginid"/>
  <s:password name="password" key="app.password"/>
  <s:select key="app.type" 
       name="type"       
       list="#{'manager':'Manager', 'clerk':'Clerk'}"/>
  <s:submit value="Login"/>
</s:form>
</td></tr>
</table>

Step 13: Create menu page for manager manager_menu.jsp
<%taglib uri="/struts-tags" prefix="s"%>
<table width="100%">
<tr>
  <td align="left" style="border-bottom: 2px solid green;">
  <s:property value="#session.loginid"/>
  </td>
</tr>
<tr>
  <td align="left"><a href="index.jsp">Home</a></td>
</tr>
<tr>
  <td align="left"><a href="manager.action">New</a></td>
</tr>
<tr>
  <td align="left"><a href="manager.action">Edit</a></td>
</tr>
<tr>
  <td align="left"><a href="manager.action">Delete</a></td>
</tr>
<tr>
  <td align="left"><a href="logoff.action">Logoff</a></td>
</tr>
</table>

Step 14: Create menu page for clerk clerk_menu.jsp
<%taglib uri="/struts-tags" prefix="s"%>
<table width="100%">
<tr>
  <td align="left" style="border-bottom: 2px solid green;"><s:property value="#session.loginid"/>
  </td>
</tr>
<tr>
  <td align="left"><a href="index.jsp">Home</a></td>
</tr>
<tr>
  <td align="left"><a href="clerk.action">Deposit</a></td>
</tr>
<tr>
  <td align="left"><a href="clerk.action">Withdraw</a></td>
</tr>
<tr>
  <td align="left"><a href="clerk.action">Transfer</a></td>
</tr>
<tr>
  <td align="left"><a href="logoff.action">Logoff</a></td>
</tr>
</table>

Step 15: Create body.jsp
<%taglib prefix="s" uri="/struts-tags"%>
<table align="center" width="100%">
<tr>
    <td>
    <h1><font color=green>You have successfully login</font></h1>
    </td>
</tr>
<tr>
    <td>You have logged in as :<b><s:property value="loginid" /></b>
    </td>
</tr>
</table>

Step 16: Create actionbody.jsp
<%taglib prefix="s" uri="/struts-tags"%>
<table width="100%" align="center" bgcolor="#F0F8FF">
   <tr>
  <td>
  <h1>This page is Under Construction</h1><br>
  Please try after some time.</td>
   </tr>
</table>

Step 17: Create properties files :
struts.properties
struts.custom.i18n.resources=ApplicationResources

ApplicationResources.properties
app.title=Struts 2 Tiles
app.loginid=Login Id
app.password=Password
app.type=Type
app.loginid.blank=Username is Required
app.password.blank=Password is Required
app.invalid=Same Loginid and Password Required

Step 18: Create action class LoginAction.java
package techmyguru.actions;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements SessionAware {
String loginid;
String password;
String type;
Map sessionMap;
@Override
public void setSession(Map sessionMap
{
this.sessionMap=sessionMap;
}
public String getLoginid() {
  return loginid;
}
public void setLoginid(String loginid) {
  this.loginid = loginid;
}
public String getPassword() {
  return password;
}
public void setPassword(String password) {
  this.password = password;
}
public String getType() {
  return type;
}
public void setType(String type) {
  this.type = type;
}
public String execute() throws Exception {
  // id and password validation logic
  // you can validate from database
  if(loginid.equals(password))
  {
  sessionMap.put("loginid",loginid);
  if("manager".equals(type))
  {
  return "manager";
  }
  else
  {
    return "clerk";
  }      
    
  }else{
    this.addActionError(getText("app.invalid"));
    return ERROR;
  }
}
public void validate() {
  if ( (loginid == null || (loginid.length() == 0) ) {
    this.addFieldError("loginid", getText("app.loginid.blank"));
  }
  if ( (password == null || (password.length() == 0) ) {
    this.addFieldError("password", getText("app.password.blank"));
  }
}  
}

Step 19: Create action LogoffAction.java
package techmyguru.actions;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class LogoffAction extends ActionSupport implements SessionAware 
{
  Map sessionMap;
  @Override
  public void setSession(Map sessionMap
  {
  this.sessionMap = sessionMap;
  }
  @Override
  public String execute() throws Exception 
  {
  sessionMap.remove("loginid");
  return SUCCESS;
  }
}

Step 20: Configure Action in struts.xml file :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="devMode" value="true" />
<package name="default" extends="tiles-default">
<action name="login" class="techmyguru.actions.LoginAction">
  <result name="error">/login.jsp</result>
  <result name="input" type="tiles">login.def</result>
  <result name="manager" type="tiles">manager.welcome.def</result>
  <result name="clerk" type="tiles">clerk.welcome.def</result>
</action>
<action name="clerk">
  <result type="tiles">clerk.def</result>
</action>
<action name="manager">
  <result type="tiles">manager.def</result>
</action>
<action name="logoff"  class="techmyguru.actions.LogoffAction">
  <result type="tiles">login.def</result>
</action>
</package>
</struts>

Step 21: Verify the project directory structure as below.
Tiles in Struts2

Step 22: Run the project and get output.
Tiles in Struts2

Click Login and enter user name , password and select role
Tiles in Struts2

You will get below screen upon successful login.
Tiles in Struts2

Below screen for clerk
Tiles in Struts2

Below screen for manager
Tiles in Struts2

It validates the user also.
Tiles in Struts2

Previous Download Source Code Next

Comment by : Sachin Gupta December 27th, 2014
Comment : Why you are using the properties files? And can you tell this project slightly in details.

Comment by : marsh November 29th, 2014
Comment : what if the request is moving from login to 5 other pages and at the 3rd page i want to use the value inside the session? and the 6th page i do logout.

Comment by : dileep July 24th, 2014
Comment : Please specify URL we need to use: I am getting 404 error like: url comming: http://localhost:8080/Struts2Tiles/ type Status report message /Struts2Tiles/ description The requested r


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code