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.
  1. 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
  2. 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.
  3. Configure web.xml by adding the Tiles 2 listener (like all listeners, it goes directly inside the root <web-app> element:
  4. <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener>
  5. Configure struts.xml by adding a <:result-types> element inside your <package> element(s):
  6. <result-types> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> </result-types>
  7. For each result that should go to a Tiles page, add a type attribute:
  8. <result type="tiles" name="input">admin.login</result>
  9. Create a tiles.xml file inside WEB-INF and configure your Tiles pages there.
  10. <?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>
This will get Tiles 2 correctly configured to work with Struts 2. At this point, you're ready for a tutorial on using Tiles 2, or info on how to migrate from Struts 1.

Labels: , , , ,


Comments:
Nice post. Thanks for the simple and straightforward overview. Wish I had found this 4 hours ago.
 
Have you solved the mystery of having Spring inject objects into tiles controllers?
 
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





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]