Home Struts Using tiles-defs.xml in Tiles Application
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Using tiles-defs.xml in Tiles Application

Advertisement
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.

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.

     

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 11, 2008

Ask Questions?    Discuss: Using tiles-defs.xml in Tiles Application   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
jsuresh
August 30, 2011
asdsadsds

http://www.roseindia.net/webpromothttp://www.roseindia.net/services/webdevelopement/web-developement-services.shtmlion/websubmission.shtmlhttp://www.roseindia.net/services/webdesigning/ Web Designing Web Redesigning Web Development Logo Design Web Design Packages Domain Registration Web Promotion Services SEO Services Search Eng. Optimization Search Eng. Submission SEO Tips SEO Portfolio Web Promotion Plans Content Development Content Development Article Writing Blog Writing News Writing SEO Copywriting Technical Documentation Article Marketing Web Hosting Services ASP.NET Hosting Unix Hosting E-Commerce Hosting Windows Hosting Hosting Plan E-Commerce Solutions CRM Home Privacy Policy © All Rights are Reserved for Rose India
maya
January 6, 2013
struts titles tutorial -- get error

when click on link in index.jsp get error: java.lang.IllegalArgumentException: Path Tiles.Example does not start with a "/" character
DMCA.com