Adding Tiles in your Struts Project
March 22nd, 2009
No comments
| Tiles is a framework for the development user interface. Tiles is enables the developers to develop the web applications by assembling the reusable tiles (jsp, html, etc..). Tiles uses the concept of reuse and enables the developers to define a template for the web site and then use this layout to populate the content of the web site. For example, if you have to develop a web site having more that 500 page of static content and many dynamically generated pages. The layout of the web site often changes according to the business requirement. In this case you can use the Tiles framework to design the template for the web site and use this template to populate the contents. In future if there is any requirement of site layout change then you have to change the layout in one page. This will change the layout of you whole web site |
| Steps To Create Tiles Application |
Tiles is very useful framework for the development of web applications. Here are the steps necessary for adding Tiles to your Struts application:
|
| Add the Tiles TLD to web.xml file Tiles can can be used with or without Struts. Following entry is required in the web.xml file before you can use the tiles tags in your application. |
<jsp-config> <taglib> <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri> <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> </taglib> </jsp-config> |
| Struts-Config.xml Changes |
<plug-in className="org.apache.struts.tiles.TilesPlugin" > <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="moduleAware" value="true" /> </plug-in> |
| The above configuration will add Tiles support in your project.My tiles definition as follows in tiles-defs.xml |
<definition name="IT.rootLayout" path="/rootLayout.jsp"> <put name="topBanner" value="top.html"/> <put name="leftMenu" value="leftMenu.html"/> <put name="Desktop" value="Desktop.jsp"/> <put name="footer" value="footer.html"/> </definition> |
| In the above code i have defined a Tile element, IT.rootLayout. The page is rootLayout.jsp and it has four components, Which is topBanner,leftMenu,Desktop,footer.My rootLayout.jsp will look like the following. |
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %> <html:html locale="true"> <head> <title>Test Tiles</title> </head> <body> <table width="100%" cellspacing="0" cellpadding="0" border="1"> <tr> <td><tiles:insert attribute="topBanner"/></td> </tr> <tr> <td><tiles:insert attribute="leftMenu"/></td> </tr> <tr> <td> <table cellspacing="0" cellpadding="0" border="1" width="100%"> <tr valign="top"> <td ><tiles:insert attribute="Desktop" /></td> </tr> </table> </td> </tr> <tr> <td><tiles:insert attribute="footer"/></td> </tr> </table> </body> </html:html> |
| To call the tile component IT.rootLayout, i”m going to add the following definition in struts-config.xml |
<action path="/IT.rootLayout" forward="IT.rootLayout"/> |
| We are done!!!! We have added a Tile definition and pages also |




















