Struts 2 and tiles integration

Template:

A template is a predefined pattern or format used as a guide to making something.

Tile:

A tile specifies a part of the whole page.

Generally a website is the combination of different components or parts. Most of these parts are common on every page and mainly one part is changed (body section). If we use these common components in every page it will increase the complexity and code duplicity. Later on if any changes have to made in the common part of the website it needs to be done in every page. To simplify this we use the concept of template. Let our website have header, footer, menu and body parts. These are known as tiles in template. A part can be of static or dynamic content. Tiles framework uses the tiles.xml file to manage all tiles.

Advantages of tiles framework:

  1. Easy modification.
  2. Code reusability.

For single tile definition in struts 2 application use following entry in web.xml:

  <listener>  
    <listener-class>
      org.apache.struts2.tiles.StrutsTilesListener
    </listener-class>  
  </listener>

For multiple tiles definition in struts 2 application use following entry in web.xml:

<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.struts2.tiles.StrutsTilesListener
 </listener-class>  
</listener>

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action">
  </head>  
  <body>
  </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">	
 
 	<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>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
	<package name="default" extends="struts-default">
		<result-types>
		    <result-type name="tiles" 
                     class="org.apache.struts2.views.tiles.TilesResult"/>
		</result-types>
		<action name="*Test" method="{1}" 
                           class="com.w3schools.action.Test">
		  <result name="success" type="tiles">welcome</result>
		  <result name="admin" type="tiles">admin</result>
		  <result name="user" type="tiles">user</result>
		</action>
	</package>
</struts>

Login.java

import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author w3schools
 */
public class Test extends ActionSupport{
	private static final long serialVersionUID = 1L;
 
	public String execute(){
		return SUCCESS;		
	}
 
	public String admin(){
		return "admin";		
	}
 
	public String user(){
		return "user";		
	}
}

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="baseLayout" template="/baseLayout.jsp">
      <put-attribute name="title"  value="Template"/>
      <put-attribute name="header" value="/header.jsp"/>
      <put-attribute name="menu"   value="/menu.jsp"/>
      <put-attribute name="body"   value="/body.jsp"/>
      <put-attribute name="footer"   value="/footer.jsp"/>
  </definition>
 
  <definition name="welcome" extends="baseLayout">
      <put-attribute name="title"  value="Welcome"/>
      <put-attribute name="body"   value="/welcome.jsp"/>      
  </definition>
 
  <definition name="admin" extends="baseLayout">
      <put-attribute name="title"  value="Admin"/>
      <put-attribute name="body"   value="/admin.jsp"/>      
  </definition>
 
  <definition name="user" extends="baseLayout">
      <put-attribute name="title"  value="User"/>
      <put-attribute name="body"   value="/user.jsp"/>      
  </definition>
 
</tiles-definitions>

welcome.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
 
<html>
    <head>
            <title>
              <tiles:insertAttribute name="title" ignore="true" />
            </title>
    </head>
    <body>
        <table border="1" cellpadding="2" cellspacing="2" align="center">
            <tr>
                <td height="25" colspan="2">
                    <tiles:insertAttribute name="header" />
                </td>
            </tr>
            <tr>
                <td height="200">
                    <tiles:insertAttribute name="menu" />
                </td>
                <td width="400">
                    <tiles:insertAttribute name="body" />
                </td>
            </tr>
            <tr>
                <td height="20" colspan="2">
                    <tiles:insertAttribute name="footer" />
                </td>
            </tr>
        </table>
    </body>
</html>

body.jsp

<h4> Body content.</h4>

header.jsp

<div align="center">
	<h4>Javawithease</h4>
</div>

footer.jsp

<div align="center">Copyright &copy; 
2014 w3schools - All rights reserved.</div>

menu.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
 
<a href="<s:url action="adminTest"/>" >Admin</a><br/>
<a href="<s:url action="userTest"/>" >User</a>

welcome.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
<html>
    <head>
        <title>Welcome Page</title>
    </head>
    <body>
        <h4>This is the welcome page.</h4>
    </body>
</html>

admin.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
<html>
    <head>
        <title>Admin Page</title>
    </head>
    <body>
        <h4>This is the admin home page.</h4>
    </body>
</html>

user.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
<html>
    <head>
        <title>User Page</title>
    </head>
    <body>
        <h4>This is the user home page.</h4>
    </body>
</html>

Output:

struts example 58 first
 
Click on Admin link.
struts example 58 admin
 
Click on User link.
struts example 58 user
 
Download this example.
 
Next Topic: Struts 2 and Quartz 2 scheduler integration with example.
Previous Topic: Struts 2 s:url and s:a data tags with example.