javastruts2tiles-3struts2-tiles-plugin

How to integrate properly Tiles 3 with Struts2 ?


I'm trying to integrate Tiles 3 with Struts 2.

I guess I've added all the necessary jar files in the lib but I get:

java.lang.ClassNotFoundException: org.apache.struts2.tiles.StrutsTilesListener

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>    
<display-name>My Application</display-name>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
    <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
    <param-value>/WEB-INF/tiles.xml</param-value>
</context-param>    
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
   </web-app>

tile.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <!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="baseLayout" template="/baseLayout.jsp">
    <put-attribute name="title" value="MyProject"></put-attribute>
    <put-attribute name="header" value=""></put-attribute>
    <put-attribute name="body" value=""></put-attribute>
    <put-attribute name="menu" value=""></put-attribute>
    <put-attribute name="footer" value=""></put-attribute>
</definition>
<definition name="homePageLayout" template="/homePageLayout.jsp">
    <put-attribute name="title" value="MyProject-HomePage"></put-attribute>
    <put-attribute name="header" value=""></put-attribute>
    <put-attribute name="leftMenu" value=""></put-attribute>
    <put-attribute name="body" value=""></put-attribute>    
    <put-attribute name="rightMenu" value=""></put-attribute>   
</definition>
</tiles-definitions>   

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>
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<constant name="struts.custom.i18n.resources" value="ApplicationResource"></constant>
<package name="default" namespace="/" extends="struts-default"> 
 <result-types>
    <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>     
  <default-action-ref name="index"/>

    <action name="ShowLogin"><result>login.jsp</result></action>
    <action name="Login" class="com.myproject.action.Login">
        <result name="success">login.jsp</result>
    </action>
</package>
</struts>

Solution

  • To integrate Tiles 3 with Struts 2 you need to use the struts2-tiles3-plugin, not the struts2-tiles-plugin.

    You are using the Tiles2 Listener:

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

    The right one is the Tiles3 Listener:

    <listener-class>
        org.apache.tiles.extras.complete.CompleteAutoloadTilesListener
    </listener-class>
    

    You are also using the wrong Struts2 Filter:

    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter
    </filter-class>
    

    Use the complete one:

    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>