java search
Loading
Help improve Java Search

Tuesday, April 01, 2008

Integrating Struts 2.0 and tiles

I am currently evaluating some web frameworks for a pet project and was trying to implement Struts 2 with tiles. Neither the Sturts 2 website, nor the tiles website gave an easy way to integrated Struts 2 and tiles. It took me a while to get them to work together. This post describes a way I figured out how to integrate Struts 2 with tiles. Struts 2 provides a plugin for integrating tiles 2. This plugin is included in the complete bundle (struts-2.x.x.x-all.zip). The following are are the steps needed to integrate struts2 with tiles.
Skip to Sample Code
  1. Download the struts complete bundle from the struts 2 website
  2. Download tiles 2 from tiles 2 website
  3. Download the tiles dependencies from the jakarta commons site
    • Commons BeanUtils 1.7.0 or above
    • Commons Digester 1.8 or above
    • Commons Logging 1.1 or above
  4. Create the layout page, and related files (except the layout, all the other files are basic jsps )
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
    <table width="100%" height="100%">
    <tr height="20%">
    <td colspan="2" align="center" bgcolor="skyblue">
    <tiles:insertAttribute name="header" /></td>
    </tr>
    <tr>
    <td bgcolor="cyan" width="75%"><tiles:insertAttribute name="body" /></td>
    </tr>
    <tr height="20%">
    <td colspan="2" align="center" bgcolor="skyblue"><tiles:insertAttribute name="footer" /></td>
    </tr>
    </table>
    </body>
    </html>
  5. Create the HelloWorld Action class
    package example;

    import com.opensymphony.xwork2.ActionSupport;

    public class HelloWorld extends ActionSupport {

    public String execute() throws Exception {
    System.out.println("Hello World");
    return SUCCESS;
    }
    }
  6. Configure the Web Deployment descriptor by adding a tiles listener to the web.xml file of your web application.
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>tilesTest</display-name>
    <listener>
    <listener-class>
    org.apache.struts2.tiles.StrutsTilesListener
    </listener-class>
    </listener>
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    </web-app>
  7. Configure struts to work with tiles, this can be done by either
    1. Extending the sturts package from "tiles-default"
      <package name="tilesTest" extends="tiles-default">
    2. OR
    3. Declaring a new "result-type", tiles, that will map to "org.apache.struts2.views.tiles.TilesResult"
      <result-types>
      <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
      </result-types>
  8. Set the type of the results in the package to "tiles"
    <result name="success" type="tiles">helloworld.home</result>

    struts.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
    <package name="tilesTest" extends="struts-default">
    <result-types>
    <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    <action name="helloWorld" class="example.HelloWorld">
    <result name="success" type="tiles">helloworld.home</result>
    </action>
    </package>
    </struts>
    Note that the result "helloword.home" must match the definition name in tiles.xml file.

  9. Create definitions for tiles in WEB-INF/tiles.xml file.
    <!DOCTYPE tiles-definitions PUBLIC
    "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
    "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
    <tiles-definitions>
    <definition name="helloworld.home" template="/layouts/layout.jsp">
    <put-attribute name="header" value="/layouts/header.jsp" />
    <put-attribute name="body" value="/index.html" />
    <put-attribute name="footer" value="/layouts/footer.jsp" />
    </definition>
    </tiles-definitions>
  10. The following is a list of jar files used for this example (copied to the WEB-INF/lib directory)
    • commons-beanutils.jar
    • commons-digester-1.8.jar
    • commons-logging-1.1.1.jar
    • freemarker-2.3.8.jar
    • ognl-2.6.11.jar
    • struts2-core-2.0.11.1.jar
    • struts2-tiles-plugin-2.0.11.1.jar
    • tiles-api-2.0.5.jar
    • tiles-core-2.0.5.jar
    • tiles-jsp-2.0.5.jar
    • xwork-2.0.4.jar
  11. This example was implemented on tomcat 6.0.16, with Java 5 update 11

8 comments:

  1. Big thx .. was really helpful

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, I like so much that u give a guide about this integration but, now I have a doubt about it... u didn't write anything about the pom.xml, and I have problems running the app... can u tell me something about it.. Thanx...

    ReplyDelete
  4. i am deploying the strut2-tiles application in tomcaat 6 i am getting the error in deployment.

    Nov 14, 2008 8:07:07 PM org.apache.catalina.core.StandardContext start
    SEVERE: Error listenerStart


    plese help me. its urgent

    ReplyDelete
  5. Damm, getting same error : SEVERE: Error listenerStart while integrating tiles2 with Struts2. Seems like incompatibility...Anyone has have it working.

    ReplyDelete
  6. Brilliant, I have been trying to get a simple installation up and running for ages and your post did it in minutes. Thanks.

    ReplyDelete