【问题标题】:How to integrate properly Tiles 3 with Struts2 ?如何将 Tiles 3 与 Struts2 正确集成?
【发布时间】:2015-08-05 05:11:13
【问题描述】:

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

我想我已经在 lib 中添加了所有必要的 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>

【问题讨论】:

标签: java struts2 tiles-3 struts2-tiles-plugin


【解决方案1】:

要将 Tiles 3 与 Struts 2 集成,您需要使用 struts2-tiles3-plugin,而不是 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>

【讨论】:

  • 感谢亲爱的@Andrea ...我删除了 struts2-tiles-plugin jar 文件,但我仍然得到同样的错误:(
  • 感谢我更改了我的 JAR 文件版本并使用了您的建议......它成功了
【解决方案2】:

看起来你错过了类路径中的struts2-tiles-plugin.jar。添加它,错误应该消失了。

【讨论】:

  • 亲爱的@Jens 我在 /WEB-INF/lib 中添加了所有必要的 Jar 文件
  • @samansv 错误清楚地表明您缺少struts2-tiles-plugin 中的类。您是添加 jars 还是使用 maven?
  • 我知道我在 /lib 中添加了 struts-tiles-plugin 和 struts-tiles3-plugin
  • @samansv struts2-tiles-plugin.jar 中的类。我可以看到它不在您使用的插件中。
【解决方案3】:

我的猜测是您缺少具有 org.apache.struts2.tiles.StrutsTilesListener 类的 jar/依赖项,即 struts2-tiles-plugin jar。看看search.maven.org

如果要下载 jar,请从同一链接下载。

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

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

【讨论】:

  • 感谢亲爱的@Reservoir,但我添加了 struts-tiles-plugin 和 struts-tiles3-plugin JAR 文件,但仍然出现相同的错误:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 2013-08-12
相关资源
最近更新 更多