Home Struts Struts Struts2.2.1 Struts2 Tiles Example

Ask Questions?

View Latest Questions


 
 

Struts2 Tiles Example
Posted on: November 24, 2011 at 12:00 AM
Advertisement
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
<listener>
	<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>

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="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

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

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"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
	<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>

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

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
<!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="*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>
		</action>
	</package>
</struts>

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:


DMCA.com