Let us try to understand how tiles are better than simple jsp include by modifying our example.
In tiles firstly we have to create Layout.jsp for the layout.
Layout.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles"%>
<html:html>
<body>
<table>
<tr>
<td>
<tiles:insert attribute="header"/>
</td>
</tr>
<tr>
<td>
<tiles:insert attribute="body"/>
</td>
</tr>
<tr>
<td>
<tiles:insert attribute="footer"/>
</td>
</tr>
</table>
<body>
</html:html>
Tiles:insert tag defines a particular location where we can place any other component. Value for this is
retrieved from the XML file which we will create later on.
The only difference is that instead of explicitly including the JSP files, the template file includes a
body-content section. This allows us to reuse this template for any page that has this generic format.
Once we figure out how to supply the page-specific body content, we can reuse this template over and
over again. This one file can then control the layout of multiple pages. If we need to modify the layout
of the site, this is the only file we need to change--that's the real power of using a Tiles framework.
Now we will create two JSPs for the body. firstBody.jsp and secondBody.jsp as shown below.
firstBody.jsp
<B>
Body for First JSP
</B>
secondBody.jsp
<B>
Body for Second JSP
</B>
Create the xml file and specify the definition of all the components. Let's name that file tileDef.xml and
specify its pages as shown below:
tileDef.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<component-definitions>
<definition name="FirstPage">
<put name="header" value="/Header.jsp" />
<put name="body" value="/firstBody.jsp"/>
<put name="footer" value="/Footer.jsp" />
</definition>
<definition name="SecondPage">
<put name="header" value="/Header.jsp" />
<put name="body" value="/secondBody.jsp"/>
<put name="footer" value="/Footer.jsp" />
</definition>
</component-definitions>
We will use these definitions (FirstPage and SecondPage) into our struts-config.xml for forwarding
purpose. But before doing that we need to include the Tiles plug-in to our application. Add the code
given below to our struts-config.xml.
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<!-- Path to XML definition file -->
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
</plug-in>
This code should be in between the struts-config tag.
Now use our defined definitions for forwarding purpose. The final struts-config.xml is shown below:
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action
path="/first"
forward="/FirstPage" />
<action
path="/second"
forward="/SecondPage" />
</action-mappings>
<controller
processorClass =
"org.apache.struts.tiles.TilesRequestProcessor"/>
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<!-- Path to XML definition file -->
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
</plug-in>
</struts-config>