Struts 2 Tiles Framework Integration Tutorial Example

We can customize the design of the struts 2 application by coordinating with tiles framework.

A page can contain numerous parts (known as tile, for example, header, left sheet, right sheet, body part, footer and so forth. In tiles structure, we deal with all the tile by our Layout Manager page.


Advantage of tiles framework

There are following focal points of tiles framework:

  • Customization by concentrated page We can customize the format of the considerable number of pages by single page (incorporated page) only.
  • Code reusability A solitary part for example header or footer can be utilized in numerous pages. So it spares coding.
  • Easy to modify If any part (tile) is altered, we don't have to change numerous pages.
  • Easy to remove If any part (tile) of the page is evacuated, we don't have to expel the code from every one of the pages. We can expel the tile from our design supervisor page.
struts 2 with tiles example

Steps to make tiles application

The steps are as follows:

  1. Add tiles library in your application
  2. Define Struts2TilesListener in web.xml file
  3. Create the information page (index.jsp)
  4. Create the Action class
  5. Extend the tiles-default bundle in your bundle and characterize all the outcome type as tiles in struts.xml file
  6. Create the tiles.xml document and characterize every one of the tiles definitions
  7. Create the LayoutManager page
  8. Create the View components

1) Add tiles library in your application

If you are utilizing myeclipse IDE, you can include tiles library by right snap on the undertaking - > Build Path - > Add Library - > Add Myeclipse Library - > Select the Struts 2 tiles library - > ok.

If you are utilizing shroud or Netbeans IDE, you have to include 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.     
  21.   <listener>  
  22.   <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>  
  23.   </listener>  
  24.     
  25.  </web-app>  

3) Create the input page (index.jsp)

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

4) Create the action class

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

Login.java
  1. package com.javacodegeeks;  
  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.javacodegeeks.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>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition