Sunday, September 2, 2007
Using Tiles 2 With Struts 2
The famous Struts MVC framework grew up with Tiles as its templating technology, and developers were used to the two being more-or-less inseperable. As version 2 of each technology rolled around, the developers decided it would be better to make each usable separately as well. They've succeeded in doing this, but it seems like less attention has been given to keeping them working together. The Tiles 2 web site implies that it only supports Struts 1, and one part of the Struts 2 web site implies that Tiles 2 is only a sandbox project. Thankfully, neither of these are true, and if you find the right place on the Struts 2 web site, the confusion is (almost) cleared up.
Here are the steps you need to go through to set up Tiles 2 in a Struts 2 webapp.
- Download Tiles 2 and put all three of its jar files into your webapp:
- tiles-api-2.x.x.jar
- tiles-core-2.x.x.jar
- tiles-jsp-2.x.x.jar
- Download the Struts 2 full binary distribution (if you haven't already) and copy struts2-tiles-plugin-2.x.x.jar from its lib directory into your webapp. Note that struts-blank.war does not include this plugin by default.
- Configure web.xml by adding the Tiles 2 listener (like all listeners, it goes directly inside the root <web-app> element:
- Configure struts.xml by adding a <:result-types> element inside your <package> element(s):
- For each result that should go to a Tiles page, add a type attribute:
- Create a tiles.xml file inside WEB-INF and configure your Tiles pages there.
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<result type="tiles" name="input">admin.login</result>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://struts.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="admin" template="/WEB-INF/layouts/admin-layout.jsp">
<put-attribute name="header" value="/WEB-INF/tiles/admin-header.jsp" />
<put-attribute name="footer" value="/WEB-INF/tiles/admin-footer.jsp" />
</definition>
<definition name="admin.login" extends="admin">
<put-attribute name="title" value="Login"/>
<put-attribute name="content" value="/WEB-INF/tiles/admin-login.jsp"/>
</definition>
<definition name="admin.listPages" extends="admin">
<put-attribute name="title" value="Pages"/>
<put-attribute name="content" value="/WEB-INF/tiles/admin-list-pages.jsp"/>
</definition>
</tiles-definitions>
Labels: configuration, struts, struts2, tiles, tiles2
Comments:
<< Home
Scott, nope, haven't tried integrating Spring and Struts yet. I'm really interested in doing so - after using Spring to wire together an application, I'm starting to wonder how anybody ever did it otherwise! :-)
You forgot to pass config param.
_context-param_
_param-name_tilesDefinitions_/param-name_
_param-value_WEB-INF/conf/struts/tiles.xml_/param-value_
_/context-param_
Post a Comment
_context-param_
_param-name_tilesDefinitions_/param-name_
_param-value_WEB-INF/conf/struts/tiles.xml_/param-value_
_/context-param_
<< Home
Subscribe to Posts [Atom]

