Recipe14.3.Displaying Tiles Using a Struts Forward


Recipe 14.3. Displaying Tiles Using a Struts Forward

Problem

You don't like having to write and maintain minimal JSP pages that insert a Tiles definition.

Solution

Use action forwards in your struts-config.xml file that specify the definition name for the path attribute on the forward element. The TilesRequestProcessor, deployed behind the scenes by the TilePlugin, will forward to the definition as if it were inserted by a JSP.

Assuming Tiles definitions are in your tiles-defs.xml file named .someTilesDef and .anotherTilesDef, you can create actions such as the following in your struts-config.xml file:

<action path="/doStartPage"      forward=".someTilesDef"/> <action path="/doPageOne"         type="com.foo.SomeAction">     <forward name="success" path=".anotherTilesDef"/> </action>

Discussion

If you've read over the first couple of Tiles recipes, you'll notice that once a definition is created, its trivial to write JSP code to render the page. Tiles uses a custom request processor to eliminate the need for these trivial pages. The TilesPlugin installs the TilesRequestProcessor when the plug-in initializes. The TilesRequestProcessor provides special handling that allows you to use a Tiles definition as the target path for a Struts forward. When the TileRequestProcessor processes the forward, if the forward path matches a Tiles definition name, it loads the definition, creates and initializes the Tile's context attributes, and inserts the corresponding definition. If the path doesn't match a definition, the TilesRequestProcessor lets the base RequestProcessor handle the request.

Using this approach you can elminate almost all direct references to JSPs in your struts-config.xml file. You will end up with many actions and definitions; for each action, you eliminate one JSP file that you would have had to maintain. Instead of n JSP pages, you will have n actions mappings in your struts-config.xml file.

Local and global forwards can use this technique. The common convention is to specify definition names using a dot (".") separator instead of a slash ("/").

See Also

For more information, check out the JavaDocs for the TilesRequestProcessor. These are included with Struts, or you can find this documentation at http://struts.apache.org/api/org/apache/struts/tiles/TilesRequestProcessor.html.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net