This post describes a way I figured out how to integrate Struts 2 with tiles. Struts 2 provides a plugin for integrating tiles 2. This plugin is included in the complete bundle (struts-2.x.x.x-all.zip). The following are are the steps needed to integrate struts2 with tiles.
- Download the struts complete bundle from the struts 2 website
- Download tiles 2 from tiles 2 website
- Download the tiles dependencies from the jakarta commons site
- Commons BeanUtils 1.7.0 or above
- Commons Digester 1.8 or above
- Commons Logging 1.1 or above
- Create the layout page, and related files (except the layout, all the other files are basic jsps )
<%@ 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=ISO-8859-1″>
<title>Insert title here</title>
</head>
<body>
<table width=“100%” height=“100%”>
<tr height=“20%”>
<td colspan=“2″ align=”center” bgcolor=“skyblue”><tiles:insertAttribute
name=“header” /></td>
</tr>
<tr>
<td bgcolor=“cyan” width=“75%”><tiles:insertAttribute name=“body” /></td>
</tr>
<tr height=“20%”>
<td colspan=“2″ align=”center” bgcolor=“skyblue”><tiles:insertAttribute
name=“footer” /></td>
</tr>
</table>
</body>
</html>
- Create the HelloWorld Action class package example; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport { public String execute() throws Exception { System.out.println(”Hello World”); return SUCCESS; } }
- Configure the Web Deployment descriptor by adding a tiles listener to the web.xml file of your web application.
<?xml version=“1.0″ >
<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns=“http://java.sun.com/xml/ns/javaee” xmlns:web=“http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=“WebApp_ID” version=“2.5″>
<display-name>tilesTest</display-name>
<listener>
<listener-class> org.apache.struts2.tiles.StrutsTilesListener </listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
- Configure struts to work with tiles, this can be done by either
1. Extending the sturts package from “tiles-default” <package name=”tilesTest” extends=”tiles-default”>OR
2. Declaring a new “result-type”, tiles, that will map to “org.apache.struts2.views.tiles.TilesResult” <result-types> <result-type name=”tiles” class=”org.apache.struts2.views.tiles.TilesResult” /> </result-types>
- Set the type of the results in the package to “tiles” <result name=”success” type=”tiles”>helloworld.home</result>struts.xml
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN” “http://struts.apache.org/dtds/struts-2.0.dtd”>
<struts>
<package name=”tilesTest” extends=”struts-default”>
<result-types>
<result-type name=”tiles”
class=”org.apache.struts2.views.tiles.TilesResult” />
</result-types>
<action name=”helloWorld” class=”example.HelloWorld”>
<result name=”success” type=”tiles”>helloworld.home</result>
</action>
</package>
</struts>
Note that the result “helloword.home” must match the definition name in tiles.xml file.
- Create definitions for tiles in WEB-INF/tiles.xml file.
<!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=”helloworld.home” template=”/layouts/layout.jsp”>
<put-attribute name=”header” value=”/layouts/header.jsp” />
<put-attribute name=”body” value=”/index.html” />
<put-attribute name=”footer” value=”/layouts/footer.jsp” />
</definition>
</tiles-definitions>