You are on page 1of 2

Struts and Tiles - Steps to use Struts and Tiles

1. Edit /WEB-INF/web.xml to reference Tiles Taglib


<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

2. Edit /WEB-INF/struts-config.xml to add TilesRequestProcessor


<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor" />

3. Setup /WEB-INF/struts-config.xml to define the Tiles Plugin


<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"/>
<set-property property="definitions-parser-validate" value="true" />
</plug-in>
Struts will load the tiles - controller and the tiles – plugin after complete steps 2 & 3
If Tiles definitions defined in a centralized file will be used, Edit /WEB-INF/struts-config.xml to instruct Struts to load the Tiles
plugin which will create the factory corresponding to the file.

4. Create a template file WebRoot/WEB-INF/tiles/layout.jsp using Tiles Tags as placeholders for title, header, navigation,
body and footer
<%@ page language="java"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title><tiles:getAsString name="title" /></title>
<link rel="STYLESHEET"
href="<html:rewrite page='/css/styles.css'/>"
type="text/css">
</head>
<body>
<table border="1" width="900" cellspacing="5">
<tbody>
<tr><td colspan="2"><tiles:insert attribute="header" /></td></tr>
<tr>
<td width="300"><tiles:insert attribute="navigation" /></td>
<td width="600"><tiles:insert attribute="body" /></td>
</tr>
<tr><td colspan="2"><tiles:insert attribute="footer" /></td></tr>
</tbody>
</table>
</body>
</html:html>

5. Create /WEB-INF/tiles-defs.xml - an empty Tiles definition file


<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
</tiles-definitions>

6. Edit /WEB-INF/tiles-defs.xml to add the base definition to the tile definition xml
<!-- Base Tiles Definition -->
<definition name="base.definition" path="/WEB-INF/tiles/layout.jsp">
<put name="title" value=""/>
<put name="header" value="/WEB-INF/tiles/header.jsp" />
<put name="navigation" value="/WEB-INF/tiles/navigation.jsp" />
<put name="body" value="" />
<put name="footer" value="/WEB-INF/tiles/footer.jsp" />
</definition>
7. Create the definition for each of the three pages by extending the above definition and overriding the empty values
<!-- Tiles Definition of welcome page -->
<definition name="welcome.page" extends="base.definition">
<put name="title" value="Welcome!"/>
<put name="body" value="/WEB-INF/tiles/index_body.jsp" />
</definition>

<!-- Tiles Definition of search page -->


<definition name="search.page" extends="base.definition">
<put name="title" value="Search"/>
<put name="body" value="/WEB-INF/tiles/search_body.jsp" />
</definition>

<!-- Tiles Definition of database error page -->


<definition name="dberror.page" extends="base.definition">
<put name="title" value="An error occured"/>
<put name="body" value="/WEB-INF/tiles/dberror_body.jsp" />
</definition>
Each of the definitions extends from the base.definition with the parameter extends=”base.definition” will reuse the header,
navigation and footer from the base.definition.

8. Create JSPs that define the layout pieces for header, navigation, footer, index_body, etc. in WebRoot/WEB-INF/, as well
as JSPs that populate the layout for index, search, and dberror, etc. in WebRoot/
a. Using titles:insert: At the top of each JSP page that will populate the layout and use the Tiles custom tags, add the
following line declaring the Tiles custom tag library for use on the page:
<%@ page language="java"%>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<%-- Servlet 2.3 can omit the declaration in WEB-INF/web.xml and replace above line with the full URI: --%>
<%-- @ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" --%>
<tiles:insert definition="welcome.page" />
b. Using lofic:forward: At the top of each JSP page that will use the Logic custom tags, add the following line declaring
the Logic custom tag library for use on the page:
<%@ page language="java"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<logic:forward name="welcome" />
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
This line include logic tag will arranges struts to find a forward with the name welcome. If the application don’t find this forward
it will leads an error.

9. Change the references to physical JSPs in struts-config.xml and replace them with one of the above names. For instance,
change all references to "/search.jsp" as "search.page".
<action-mappings>
<action path="/Welcome"
forward="welcome.page"/> <!-- forward="/index.jsp"/> -->
</action>
</action-mappings>

You might also like