遇到总结一下项目中有关Structs2升级中遇到的坑。项目大概开始于09年左右,维护近10年,由于Structs2安全漏洞问题决定升级版本,由于版本跨度比较大,一些方法已经弃用或配置变更等
// old
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.1.8.1</version>
</dependency>
// new
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.17</version>
</dependency>
<filter>
<filter-name>ActionContextCleanUp</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>ActionContextCleanUp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/project/*</url-pattern>
</filter-mapping>
在Structs2 2.5.17中 org.apache.struts2.dispatcher.FilterDispatcher
和 org.apache.struts2.dispatcher.ActionContextCleanUp
被废除,用 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
替换
java.lang.NoSuchMethodError: ognl.SimpleNode.isEvalChain(Lognl/OgnlContext;)Z
ognl
的jar包冲突
需要查询项目中Jar依赖关系,排除无用Jar包。Ognl版本至少在 3.0.6
以上
// 我遇到的是xwork中有低版本的ognl包,故排除
<exclusion>
<artifactId>ognl</artifactId>
<groupId>opensymphony</groupId>
</exclusion>
There is no Action mapped for namespace [/] and action name [user!add] associated with context path
// struts.xml配置添加
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
......
<package name="default" extends="struts-default">
......
<global-allowed-methods>regex:.*</global-allowed-methods>
......
</package>
</struts>
// action配置
<action name="login_*" method="{2}" class="com.wxmimperio.struts.{1}Action">
.......
<result name="success">/pages/success.jsp</result>
<result name="error">/pages/error.jsp</result>
......
<allowed-methods>regex:.*</allowed-methods>
......
</action>
这里在action的name中通配了一个login_*,它对应映射的是method属性。如果在客户端发生一个这样的请 求:login_init.action、login_show.action等,这时服务器就会自动调用这个action中的init()方法或 show()方法。这里的method=”{1}”代表是第一个星号,如果有多个星号,就要根据顺序来通配{1},{2},{3}….
allowed-methods
中可以用逗号分隔指定方法名,也可以用正则匹配。
错误: Struts2 与 Servlet 冲突
表现在当struts.xml如下配置:
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts拦截器会拦截/*下所有路径,所以自定义的servlet无法被mapped到,导致请求根本无法响应
解决方案如下:
<servlet>
<servlet-name>jqueryAjaxServlet</servlet-name>
<servlet-class>com.clzhang.sample.struts2.servlet.jQueryAjaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jqueryAjaxServlet</servlet-name>
<url-pattern>/servlet/jqueryAjax.servlet</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
<constant name="struts.action.extension" value="action"></constant>
转载请注明出处