struts2.3 升級 struts2.5

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.1
Maven: org.apache.struts:struts2-core:2.5.10.1
Maven: org.apache.struts:struts2-json-plugin:2.5.10.1
Maven: org.apache.struts:struts2-spring-plugin:2.5.10.1

Maven: org.springframework:spring-aop:4.1.6.RELEASE
Maven: org.springframework:spring-beans:4.1.6.RELEASE
Maven: org.springframework:spring-context:4.1.6.RELEASE
Maven: org.springframework:spring-context-support:4.1.6.RELEASE
Maven: org.springframework:spring-core:4.1.6.RELEASE
Maven: org.springframework:spring-expression:4.1.6.RELEASE
Maven: org.springframework:spring-jdbc:4.1.6.RELEASE
Maven: org.springframework:spring-jms:4.1.6.RELEASE
Maven: org.springframework:spring-messaging:4.1.6.RELEASE
Maven: org.springframework:spring-orm:4.1.6.RELEASE
Maven: org.springframework:spring-oxm:4.1.6.RELEASE
Maven: org.springframework:spring-test:4.1.6.RELEASE
Maven: org.springframework:spring-tx:4.1.6.RELEASE
Maven: org.springframework:spring-web:4.1.6.RELEASE
Maven: 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,所有方法是返回的MAP
Map<String, Object> parameters = invocation.getInvocationContext().getParameters();

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

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

HttpParameters 類型為 Map<String, Parameter>
Parameter 只能被動使用,不能主動創建

@Override
public 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低版本進行了編譯打包~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容