Struts 2 Tiles Framework Integration Tutorial Example

Struts 2 tiles framework integration tutorial example. Let's see the simple example to integrate struts 2 application with tiles framework.

Struts 2 Tiles Framework Integration Tutorial Example

Struts 2 Tiles Framework Integration Tutorial Example

We can customize the layout of the struts 2 application by integrating with tiles framework.

A web page can contain many parts (known as tile) such as header, left pane, right pane, body part, footer etc. In tiles framework, we manage all the tile by our Layout Manager page.


Advantage of tiles framework

There are following advantages of tiles framework:

  • Customization by centralized page We can customize the layout of all the pages by single page (centralized page) only.
  • Code reusability A single part e.g. header or footer can be used in many pages. So it saves coding.
  • Easy to modify If any part (tile) is modified, we don't need to change many pages.
  • Easy to remove If any part (tile) of the page is removed, we don't need to remove the code from all the pages. We can remove the tile from our layout manager page.

Steps to create tiles application

The steps are as follows:

  1. Add tiles library in your application
  2. Define Struts2TilesListener in web.xml file
  3. Create the input page (index.jsp)
  4. Create the Action class
  5. Extend the tiles-default package in your package and define all the result type as tiles in struts.xml file
  6. Create the tiles.xml file and define all the tiles definitions
  7. Create the LayoutManager page
  8. Create the View components

1) Add tiles library in your application

If you are using myeclipse IDE, you can add tiles library by right click on the project -> Build Path -> Add Library -> Add Myeclipse Library -> Select the Struts 2 tiles library -> ok.

If you are using eclipse or Netbeans IDE, you need to add the required tiles library in your project.

2) Define Struts2TilesListener in web.xml file

Provide entry of listener class Struts2TilesListener in the web.xml file.

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <filter>  
  11.    <filter-name>struts2</filter-name>  
  12.    <filter-class>  
  13.     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  14.    </filter-class>  
  15.   </filter>  
  16.   <filter-mapping>  
  17.     <filter-name>struts2</filter-name>  
  18.     <url-pattern>/*</url-pattern>  
  19.   </filter-mapping>
  20.   <listener>  
  21.   <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>  
  22.   </listener>
  23.  </web-app>  

3) Create the input page (index.jsp)

index.jsp

  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <s:form action="login">  
  3. <s:textfield name="name" label="Name"></s:textfield>  
  4. <s:password name="password" label="Password"></s:password>  
  5. <s:submit value="login"></s:submit>  
  6. </s:form>  

4) Create the action class

This action class contains one field name and defines the execute method.

Login.java

  1. package com.samtpoint;  
  2.   
  3. public class Login {  
  4. private String name,password;  
  5.   
  6. //getters and setters  
  7.   
  8. public String execute(){  
  9. if(password.equals("admin")){  
  10.     return "success";  
  11. }  
  12. else{  
  13.     return "error";  
  14. }  
  15. }  
  16. }  

5) Inherit the tiles-default package and define all the result type as tiles in struts.xml

This xml file defines one package with one action and two results.

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"   
  3. "http://struts.apache.org/dtds/struts-2.1.dtd">  
  4. <struts>  
  5.   
  6.   
  7. <package name="abc" extends="tiles-default" >  
  8.   
  9. <action name="login" class="com.samtpoint.Login">  
  10. <result name="success" type="tiles">login-success</result>  
  11. <result name="error" type="tiles">login-error</result>  
  12. </action>  
  13.    
  14. </package>  
  15. </struts>      

6)Create the tiles.xml file and define all the tiles definitions

The tiles.xml file must be located inside the WEB-INF directory.

tiles.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2.   
  3. <!DOCTYPE tiles-definitions PUBLIC   
  4. "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"  
  5.  "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">   
  6.   
  7. <tiles-definitions>  
  8.      
  9.    <definition name="login-success" template="/layoutmanager.jsp">   
  10.    <put-attribute name="title" value="Welcome Page"/>   
  11.    <put-attribute name="body" value="/login-success.jsp"/>   
  12.    </definition>  
  13.      
  14.    <definition name="login-error" template="/layoutmanager.jsp">   
  15.    <put-attribute name="title" value="Login Error"/>   
  16.    <put-attribute name="body" value="/login-error.jsp"/>   
  17.    </definition>   
  18.      
  19. </tiles-definitions>  

7) Create the LayoutManager page

It is the layout manager page. It used getAsString tag of tiles to include the string resource and insertAttribute tag of tiles to include the page resource.

layoutmanager.jsp

  1. <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  3.  "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <title><tiles:getAsString name="title" /></title>  
  7. </head>  
  8. <body>  
  9.   
  10. <%@  include file="header.jsp" %>  
  11. <tiles:insertAttribute name="body" />  
  12. <%@ include file="footer.jsp" %>  
  13.   
  14. </body>  
  15. </html>   

8)Create View components

There are many view components such as header.jsp, footer.jsp, welcome.jsp etc.


header.jsp

  1. <h2 style="background-color:pink;text-align:center;">It is header tile</h2>  
  2. <hr/>  

footer.jsp

  1. <hr>  
  2. <h2 style="background-color:pink;text-align:center;">It is footer tile</h2>  

login-success.jsp

  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. Welcome, <s:property value="name"/>  
  4. </textrea></div>  
  5. <hr/>  
  6. <strong>login-error.jsp</strong>  
  7. <div class="codeblock"><textarea name="code" class="xml" >  
  8. Sorry, username or password error!  
  9. <jsp:include page="index.jsp"></jsp:include>  

Output

The password is not admin, so the error page will be displayed.

If a password is admin, a success page will be displayed.


How to define multiple tiles files in struts 2 application

To define multiple tiles, you need to add the following entry in your web.xml file.

  1. <context-param id="struts_tiles">  
  2.  <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>  
  3.  <param-value>/WEB-INF/tiles1.xml,/WEB-INF/tiles2.xml</param-value>  
  4. </context-param>