如何将 Tiles 3 与 Struts2 正确集成?

How to integrate properly Tiles 3 with Struts2 ?

我正在尝试将 Tiles 3 与 Struts2 集成。

我想我已经在库中添加了所有必要的 jar 文件,但我得到:

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>

您似乎错过了类路径中的 struts2-tiles-plugin.jar。添加它,错误应该消失了。

我猜你错过了 jar/dependency,它有 org.apache.struts2.tiles.StrutsTilesListener class,也就是 struts2-tiles-plugin jar。看看 search.maven.org

如果要下载 jar,请从同一个 link 下载。

如果您使用的是 maven,请添加此依赖项。

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-tiles-plugin</artifactId>
    <version>2.5-BETA1</version>
</dependency>

要将 Tiles 3 与 Struts 2 集成,您需要使用 struts2-tiles3-plugin, not the struts2-tiles-plugin.

您正在使用 Tiles2 侦听器:

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

右边的是Tiles3 Listener:

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

你也用错了Struts2过滤器:

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

使用完整的:

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