Struts 2 and Tiles 2 integration and configuration

Recently i while doing a R&D sort of thing on my ide’s (eclipse and netbeans) i encountered on a struts 2 application.I read its documentation on the page and decided to give it a try as i went through the features.Getting started with struts 2 was a piece of cake , more ever with the site-mesh integration it just blew me off.It worked fine.However off late i encountered a requirement stating usage of tiles with struts 2 .Initially i thought of it as easy but as i ususally feel of easy things , it turned out to be real pain.Eventually a couple of hours of fight saw me through the struts 2 defences and led to a good integration of tiles2 with struts .This is a walk through is to achieve the same.The versions for struts 2 and tiles 2 used are 2.0.6 and any further versions haven’t been tested by me.

Assumptions:

  • JDK 1.5 installed
  • Tomcat 6.0 installed
  • You can create a struts 2 web application in eclipse or any other ide.

The process involves the following steps:

  1. Download the binaries
  2. Changing the configuration files for application.

1.Download the binaries

I have used struts 2.0.6 libraries , which can be downloaded from Here .

Tiles 2.0.6 binaries can be downloaded from Here

Extract the jar’s from both the zip files and put them in your lib directory of your web project.

2. Changing the configuration files for application.

This involves changing web.xml ,struts.xml and tiles .xml

web.xml

Add following section to web.xml

<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>

<context-param>

<param-name>tilesDefinitions</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>

struts.xml
add following section to struts.xml

<package name=”org.action” extends=”tiles-default” >
<action name=”myaction” class=”org.action.YourAction”>
<result name=”INPUT” type=”tiles”>tiles:test</result>
</action>
</package>

here “org.action” is the package name in which the action class resides.

tiles.xml

add the following snippet to tiles.xml file within “tiles-definitions”

<definition name=”tiles:layout” template=”/WEB-INF/pages/layout.jsp”>
<put name=”body” value=”"/>
</definition>
<definition name=”tiles:test” extends=”tiles:layout”>
<put name=”body” value=”/WEB-INF/pages/test.jsp”/>
</definition>

Rest of the struts 2 application remains unaltered. Note that tiles.xml should be in WEB-INF folder as mentioned in the web.xml.The jsp’s and action should be present at correct locations and its all done .

p.s : Due to frequent requests for the sample app , i have decided to provide one simplest example of the integration .However the struts 2 and tiles version differ from the ones explained earlier .The file can be found at the URL Here or Here

~ by jayeshpowar on July 4, 2008.

11 Responses to “Struts 2 and Tiles 2 integration and configuration”

  1. hi i am george developer i am using tiles 2 and struts 2 when clicking on the menu in my application it loading all pages ex; header, body, and footer content i want to refresh only blody area .is it posible in tiles 2 if it is posible any body can help me yo solve my problem
    Regards
    George

    • Hi George,
      Sorry for the late reply.I m not an expert in tiles 2 . But so far what i have used in tiles 2 , I think the way you want the pages to load would require you to load the body area only using either ajax or iframes (wouldn’t recommend using this option ).

  2. Thank you, thats the best migration guide i found!!!!

  3. Hi,
    I am struggling with struts2 + tiles. Can you share your sample app?

    • Sorry arjun , its sort of late since i have integrated the app. I have sort of lost the track of the app . I can perhaps try to get another sample working for uploading .

  4. hi , do you have a sample plz .
    thank u

  5. This is barani kumar
    I am tring to use tiles2 in struts2
    If any have sample code for tiles2,please send to me
    My id:vbaranikumar@gmail.com

  6. Good one. Thank you :)

  7. thank u

  8. Hi,
    I like this tutorial, please send me the sample code or the war file at my email :

    majidnakit@yahoo.com

    thanks lot

  9. good one

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.