Struts and Tiles - Steps To Use Struts and Tiles
Struts and Tiles - Steps To Use Struts and Tiles
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>
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>
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>