struts2.3 升级 struts2.5

来源:互联网 发布:淘宝卖家不参加双十一 编辑:程序博客网 时间:2024/05/21 04:25

1、版本

<struts2.version>2.5.10.1</struts2.version><spring.version>4.1.6.RELEASE</spring.version>

2、相关jar说明

Maven: org.apache.struts:struts2-convention-plugin:2.5.10.1Maven: org.apache.struts:struts2-core:2.5.10.1Maven: org.apache.struts:struts2-json-plugin:2.5.10.1Maven: org.apache.struts:struts2-spring-plugin:2.5.10.1Maven: org.springframework:spring-aop:4.1.6.RELEASEMaven: org.springframework:spring-beans:4.1.6.RELEASEMaven: org.springframework:spring-context:4.1.6.RELEASEMaven: org.springframework:spring-context-support:4.1.6.RELEASEMaven: org.springframework:spring-core:4.1.6.RELEASEMaven: org.springframework:spring-expression:4.1.6.RELEASEMaven: org.springframework:spring-jdbc:4.1.6.RELEASEMaven: org.springframework:spring-jms:4.1.6.RELEASEMaven: org.springframework:spring-messaging:4.1.6.RELEASEMaven: org.springframework:spring-orm:4.1.6.RELEASEMaven: org.springframework:spring-oxm:4.1.6.RELEASEMaven: org.springframework:spring-test:4.1.6.RELEASEMaven: org.springframework:spring-tx:4.1.6.RELEASEMaven: org.springframework:spring-web:4.1.6.RELEASEMaven: org.springframework:spring-webmvc:4.1.6.RELEASE

注意:需要删除

Maven: org.apache.struts.xwork:xwork-core:2.3.32

strut2.5中,xwork-core是与struts2-core合并的 pom文件中,需要忽略这个jar文件

<exclusions>    <exclusion>        <groupId>org.apache.struts.xwork</groupId>        <artifactId>xwork-core</artifactId>    </exclusion></exclusions>

3、相关代码说明

替换struts-tags.tld文件,最新的在strut2-core.jar META-INF中
替换security.tld文件,最新的在spring-security-taglibs-3.2.4.RELEASE.jar META-INF中

web.xml

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>替换为<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class><filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
原:这用到xwork-core 的jar,所有方法是返回的MAPMap<String, Object> parameters = invocation.getInvocationContext().getParameters();

现在:用到的是stuts2-core-2.5.10.1.jar

拦截器中:invocation.getInvocationContext().getParameters()返回的是HttpParameters parameters = invocation.getInvocationContext().getParameters();

HttpParameters 类型为 Map

@Overridepublic Parameter put(String key, Parameter value) {    throw new IllegalAccessError("HttpParameters are immutable, you cannot put value directly!");}HttpParameters  不支持put,会直接抛出异常,用到的同学请注意了。。。

严重:struts2.5中,不在默认使用通配符进行访问action中的方法(http://url!方法名称.html), 不默认支持”!”

如果需要使用

> 第一种

<struts><package name="default" namespace="/" extends="struts-default">   <global-allowed-methods>regex:.*</global-allowed-methods>    <action name="helloworld" class="com.imooc.action.HelloWorldAction">        <result>/result.jsp</result>        <result name="add">/add.jsp</result>        <result name="update">/update.jsp</result>    </action></package><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant></struts>

增加:

<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

action配置上面:

<global-allowed-methods>regex:.*</global-allowed-methods>

> 第二种
@AllowedMethods(value = {"方法名称1","方法名称2"})
Action中增加注解,注册Action中的方法
示例:

@AllowedMethods(value = {"save","update"})

Paste_Image.png

结语

如果在启动的时候发现有注解的错误~~
很有可能是引用的其它项目的jar文件,中使用struts2-core低版本进行了编译打包~

原创粉丝点击