Friday, January 20, 2006

Struts tiles

When making a more complex web application, you can use tiles to format your web layout. Instead of making a link to a jsp page, you 'll link to a tile which is a split up of the screen into more different jsp pages.

There is a tiles plugin for struts, you can include it in the struts-config.xml file.
When you are working module based, also the tiles are module based in each struts-config.xml file.

An example:
WEB-INF/struts-config.xml
   <plug-in className="org.apache.struts.tiles.TilesPlugin" >
      <!-- Path to XML definition file -->
      <set-property property="definitions-config"
         value="/WEB-INF/tiles-defs.xml" />
      <!-- Set Module-awareness to true -->
      <set-property property="moduleAware" value="true" />
   </plug-in>

The tiles are defined in the file:
WEB-INF/tiles-defs.xml
   <definition name="base.Tile4Rows" path="/view/tiles/Tile4Rows.jsp">
      <put name="header" value="/view/common/template.jsp" />
      <put name="navigation" value="/view/common/template.jsp" />
      <put name="body" value="/view/common/template.jsp" />
      <put name="footer" value="/view/common/template.jsp" />
   </definition>
This is a base tile containing 4 jsp pages.
Please notice that the value of the pages are defined from the root !!
Ex. /view/common/template.jsp
The base if this tile is a jsp page Tile4Rows.jsp
In this page which is a html with the tiles pages placed as:
view/tiles/Tile4Rows.jsp
   <tiles:insert attribute="body" />

The mapping to these tiles is identical as a normal mappig
WEB-INF/struts-config.xml
   <action
      path="/startWebPage"
      type="tiles.TilesForward">
      <forward name="success"       path="Homeframe"/>
   </action>

The Homeframe that is called here can also be found in the
WEB-INF/tiles-def.xml
   <definition name="Homeframe" extends="base.Tile4Rows">
      <put name="header" value="/view/common/header.jsp" />
      <put name="navigation" value="/view/common/menu.jsp" />
      <put name="body" value="base.Tile2Columns" />
      <put name="footer" value="footerframe" />
   </definition>>


Note: the "body" of this tile is again split up in a new tile base.Tile2Columns and so on...

No comments: