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.

|
Current Comments
24 comments so far (post your own) View All Comments Latest 10 Comments:I would like to Display dynamic content on alert box. Is it possible?
Posted by Amit Kumar Mishra on Thursday, 04.3.08 @ 18:28pm | #55235
Please give How to set CLASSPATH IN STRUTS TILE
THANK YOU
Posted by mahender on Saturday, 12.29.07 @ 17:50pm | #44137
if you set up your web.xml correctly with the taglibs you can do the following to use tiles defintions.
<%@ taglib uri="taglibs/struts-tiles" prefix="tiles"%>
<tiles:insert definition="your.layout" >
<tiles:put name="content" value="foo.jsp"/>
</tiles:insert>
Posted by muz on Tuesday, 10.16.07 @ 18:02pm | #34213
How to implements the stutsframe work in 'Eclipse'
Posted by K.Naga Krishna on Friday, 06.29.07 @ 08:02am | #20359
This is the best tutorial people really who wants to learn struts.step by step explination with examples makes to understand more easily.
Posted by sharon on Tuesday, 06.19.07 @ 17:11pm | #19706
its good book for beginers.
Posted by vidyadhar on Friday, 06.8.07 @ 21:12pm | #18625
How to use definations without creating defination for every single content pages.
Posted by bay1234 on Thursday, 05.31.07 @ 18:32pm | #17865
Hi,
I would like to use tiles for dynamic action, it means that I have 2 tiles template, each of them will set different action name to the JSP.
for example:
For mobile product I set the action
<definition name="mobile" template="/search.jsp">
<put name="actionName" value="/mobileSearch.do"/>
</definition>
For land line:
<definition name="land" template="/search.jsp">
<put name="actionName" value="/landSearch.do"/>
</definition>
in the jsp I set the action name like that:
<html:form action="<tiles:getAsString name="actionName"/>">
.
.
</html:form>
I got an exception.
How can I do that using tiles?
Is it possible?
Thanks,
Jaron
Posted by Jaron on Thursday, 05.10.07 @ 13:20pm | #15560
In Struts Tiles definition,we have to create our own tiles-def.xml file or it will be there in the project itself..Please do help on this
Posted by Leena on Friday, 03.30.07 @ 10:41am | #12968
Hey Monib,
Create the following chain-config.xml in your WEB-INF directory in addition to what Rajesh said:
<?xml version="1.0" ?>
<!--
Copyright 2002,2004 The Apache Software Foundation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
This file contains definitions of the standard Chain Of Responsibility
chains that emulate Struts 1.2 processing functionality. These chains
are defined in a catalog named "struts" so that the application can
use the default catalog for its own purposes, without any potential for
name clashes.
$Id$
-->
<catalog name="struts">
<define name="lookup"
className="org.apache.commons.chain.generic.LookupCommand"/>
<!-- ========== Servlet Complete Request Chain ========================= -->
<chain name="servlet-standard">
<!-- Establish exception handling filter -->
<command
className="org.apache.struts.chain.commands.ExceptionCatcher"
catalogName="struts"
exceptionCommand="servlet-exception"/>
<lookup
catalogName="struts"
name="process-action"
optional="false"/>
<lookup
catalogName="struts"
name="process-view"
optional="false"/>
</chain>
<!-- ========== Action Processing chain ======================== -->
<chain name="process-action">
<!--
This chain attempts to emulate (most of) the standard request
processing in the standard org.apache.struts.action.RequestProcessor
class, by performing the corresponding tasks in individual Commands
that are composable. The following list defines a cross reference
between the processXxx methods and the Commands that perform the
corresponding functionality:
processMultipart Integrated into servlet and legacy classes
processPath SelectAction (which also does processMapping)
processException ExceptionCatcher / ExceptionHandler
processLocale SelectLocale
processContent SetContentType
processNoCache RequestNoCache
processPreprocess LookupCommand with optional="true". Multiple
occurrences of this can easily be added, to
support additional processing hooks at any
point in the chain without modifying the
standard definition.
processCachedMessages RemoveCachedMessages
processMapping SelectAction (which also does processPath)
processRoles AuthorizeAction
processActionForm CreateActionForm
processPopulate PopulateActionForm
processValidate ValidateActionForm / SelectInput
processForward SelectForward
processInclude SelectInclude / PerformInclude
processActionCreate CreateAction
processActionPerform ExecuteAction
-->
<!-- Look up optional preprocess command -->
<lookup
catalogName="struts"
name="servlet-standard-preprocess"
optional="true"/>
<!-- Identify the Locale for this request -->
<command
className="org.apache.struts.chain.commands.servlet.SelectLocale"/>
<!-- Set (if needed) the URI of the original request -->
<command
className="org.apache.struts.chain.commands.servlet.SetOriginalURI"/>
<!-- Set (if needed) no cache HTTP response headers -->
<command
className="org.apache.struts.chain.commands.servlet.RequestNoCache"/>
<!-- Set (if needed) the HTTP response content type -->
<command
className="org.apache.struts.chain.commands.servlet.SetContentType"/>
<!-- Remove messages cached in the Session -->
<command
className="org.apache.struts.chain.commands.RemoveCachedMessages"/>
<!-- Identify the ActionConfig for this request -->
<command
className="org.apache.struts.chain.commands.servlet.SelectAction"/>
<!-- Authorize the selected ActionConfig for this request -->
<command
className="org.apache.struts.chain.commands.servlet.AuthorizeAction"/>
<!-- Create (if needed) the ActionForm for this request -->
<command
className="org.apache.struts.chain.commands.CreateActionForm"/>
<!-- Populate the ActionForm for this request -->
<command
className="org.apache.struts.chain.commands.servlet.PopulateActionForm"/>
<!-- Validate the ActionForm for this request -->
<command
className="org.apache.struts.chain.commands.servlet.ValidateActionForm"/>
<!-- Select the appropriate ForwardConfig for return to input page -->
<command
className="org.apache.struts.chain.commands.servlet.SelectInput"/>
<!-- Lookup and execute a chain command if the current ActionConfig is
so-configured. -->
<command
className="org.apache.struts.chain.commands.ExecuteCommand"/>
<!-- Select the appropriate ForwardConfig for action mappings that only
have an ActionForward -->
<command
className="org.apache.struts.chain.commands.servlet.SelectForward"/>
<!-- Select the include uri (if any) for the current action mapping -->
<command
className="org.apache.struts.chain.commands.SelectInclude"/>
<!-- Perform the include (if needed) -->
<command
className="org.apache.struts.chain.commands.servlet.PerformInclude"/>
<!-- Create (if needed) the Action for this request -->
<command
className="org.apache.struts.chain.commands.servlet.CreateAction"/>
<!-- Execute the Action for this request -->
<command
className="org.apache.struts.chain.commands.servlet.ExecuteAction"/>
</chain>
<!-- ========== View Processing chain ======================== -->
<chain name="process-view">
<!-- Lookup and execute a chain command if the current ForwardConfig is
so-configured. -->
<command
className="org.apache.struts.chain.commands.ExecuteForwardCommand"/>
<command
className="org.apache.struts.tiles.commands.TilesPreProcessor"/>
<!-- Follow the returned ForwardConfig (if any) -->
<command
className="org.apache.struts.chain.commands.servlet.PerformForward"/>
</chain>
<!-- ========== Servlet Exception Handler Chain ======================== -->
<chain name="servlet-exception">
<!--
This chain is designed to be invoked (by o.a.s.c.ExceptionCatcher)
if an unhandled exception is thrown by any subsequent command
in a processing chain (including the one that invokes a Struts
action). The standard definition of this chain supports the
exception mapping of Struts 1.1, but can be replaced in order
to handle exceptions differently.
-->
<!-- Execute the configured exception handler (if any) -->
<command
className="org.apache.struts.chain.commands.servlet.ExceptionHandler"/>
<!-- Lookup and execute a chain command if the current ForwardConfig is
so-configured. -->
<command
className="org.apache.struts.chain.commands.ExecuteForwardCommand"/>
<command
className="org.apache.struts.tiles.commands.TilesPreProcessor"/>
<!-- Follow the returned ForwardConfig (if any) -->
<command
className="org.apache.struts.chain.commands.servlet.PerformForward"/>
</chain>
</catalog>
Posted by Joel Luellwitz on Friday, 03.16.07 @ 01:17am | #11852