Using tiles-defs.xml in Tiles Application
In the last section we studied how to forward the request
(call) to a jsp page which specifies which tiles layout definition should be
used to apply to the content. In this section I will show you how to use the a
definition in the tiles-defs.xml for generating the content.
In Tiles we can define the definition in the tiles-defs.xml
which specifies the different components to "plugin" to generate the
output. This eliminates the need to define extra jsp file for each content file.
For example in the last section we defined example.jsp to display the content of
content.jsp file. In this section I will show you how to eliminate the need of
extra jsp file using tiles-defs.xml file.
Steps to Use the tiles-defs.xml
Setup the Tiles plugin in struts-config.xml file.
Add the following code in the struts-config.xml (If not present). This enables
the TilesPlugin to use the /WEB-INF/tiles-defs.xml file.
<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> |
Defining the tiles-defs.xml
In this file we are defining the different
components to "plugin". Here is the code:
<definition name="Tiles.Example"
page="/tiles/template.jsp">
<put name="title" type="string" value="Welcome" />
<put name="header" value="/tiles/top.jsp" />
<put name="menu" value="/tiles/left.jsp" />
<put name="body" value="/tiles/content.jsp" />
<put name="bottom" value="/tiles/bottom.jsp" />
</definition>
The name of the definition is Tiles.Example, we will
use this in struts-config.xml (While creating forwards in struts-config.xml
file) file. The page attribute defines the template file to be used and the put
tag specifies the different components to "plugin". Your tiles-defs.xml
should look like:
<?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>
<definition name="Tiles.Example"
page="/tiles/template.jsp">
<put name="title" type="string" value="Welcome" />
<put name="header" value="/tiles/top.jsp" />
<put name="menu" value="/tiles/left.jsp" />
<put name="body" value="/tiles/content.jsp" />
<put name="bottom" value="/tiles/bottom.jsp" />
</definition>
<definition name="${YOUR_DEFINITION_HERE}">
</definition>
</tiles-definitions>
|
Configure the Struts Action to use Tiles Definition
Open the struts-config.xml file and add the
following code:
<action path="/Tiles/Example"
forward="Tiles.Example"/> |
With Tiles, the action points to the Tiles definition,
as shown in the above code. In this code we are using the Tiles.Example
definition which we have defined in the tiles-defs.xml file. Without
Tiles, forward and action definitions point directly to JSPs. With Tiles, they
point to the page's definition in the Tiles configuration file.
Testing the
Application
Create a link in index.jsp to call the Example.
Code added are:
<li>
<html:link page="/Tiles/Example.do">Using tiles-defs.xml</html:link>
<br>
Example shows you how to use tiles-defs.xml file.
</li> |
To test the application build it using ant and deploy
on the JBoss server. Type http://localhost:8080/strutstutorial/index.jsp
in the bowser and select the Using tiles-defs.xml link. Your browser should show
the page.

|