Struts2 Tiles Example
Posted on: November 24, 2011 at 12:00 AM
In this tutorial you learn that how to use struts2 tiles result and tiles plugins

Struts2 Tiles Example

The Following are the steps for Stuts tiles plugin

1. Map the Tiles Listener class deployment descriptor(web.xml) file

2. Write a baseLaout file that arranges the tiles. The typical baseLayout file will look as given below 2. Write a XML file called tiles.xml in the WEB-INF directory. This XML file defines the tiles used in the application as
<%@ taglib uri="" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title><tiles:insertAttribute name="title" ignore="true" /></title>
	     <table border="1" cellpadding="2" cellspacing="2" align="center">
		    <td height="30" colspan="2">
			<tiles:insertAttribute name="header" />
		    <td height="250">
			<tiles:insertAttribute name="menu" />
		    <td width="350">
			<tiles:insertAttribute name="body" />
		    <td height="30" colspan="2">
			<tiles:insertAttribute name="footer" />

3. Write a xml file called tiles.xml that defines the tiles of the application and save it into the WEB-INF directory. The typical tiles.xml file will look as]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"

	<definition name="baseLayout" template="/layouts/baseLayout.jsp">
		<put-attribute name="title" value="Template" />
		<put-attribute name="header" value="/templates/header.jsp" />
		<put-attribute name="menu" value="/templates/menu.jsp" />
		<put-attribute name="body" value="/templates/body.jsp" />
		<put-attribute name="footer" value="/templates/footer.jsp" />

	<definition name="home" extends="baseLayout">
		<put-attribute name="title" value="Home Page" />
		<put-attribute name="body" value="/pages/home.jsp" />
	<definition name="aboutUs" extends="baseLayout">
		<put-attribute name="title" value="About Us Page" />
		<put-attribute name="body" value="/pages/aboutUs.jsp" />

4. Write a action class whose number of methods equals to number of defnition name you used in your application. The typical action class will look as
package net.roseindia.action;

import com.opensymphony.xwork2.ActionSupport;

public class TilesAction extends ActionSupport {

	public String home() {
		return "home";

	public String aboutUs() {
		return "aboutUs";

5. Map the above action class in the struts.xml file and also defines the tiles result as
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

	<package name="default" extends="struts-default">
			<result-type name="tiles"
			class="org.apache.struts2.views.tiles.TilesResult" />
		<action name="*Page" method="{1}" class="net.roseindia.action.TilesAction">
			<result name="home" type="tiles">home</result>
			<result name="aboutUs" type="tiles">aboutUs</result>
			<result name="contactUs" type="tiles">contactUs</result>
			<result name="registration" type="tiles">registration</result>

6. Then write the header.jsp, footer.jsp, menu.jsp, body.jsp and other JSP file specific to your application. When you will run the application the application will typically look as

Download The Sample Application

Related Tags for Struts2 Tiles Example:



Advertisement null