struts2.3之後的版本,配置上以及包都有些改動,我是從2.0.9版本升級到2.5版本,遇到了不少坑,下面記錄一下升級過程。
首先,升級到版本需要修改的配置文件主要有一下文件:
1. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>totoro</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- struts2 config --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- spring config --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
2. strtus.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 指定默認編碼集 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 設置瀏覽器是否緩存靜態內容,開發階段應關閉,生產階段打開,默認爲打開 --> <constant name="struts.serve.static.browserCache" value="false"></constant> <!-- 當struts的配置文件修改後,系統是否自動重新加載該文件,默認爲false,開發階段應打開 --> <constant name="struts.configuration.xml.reload" value="true"></constant> <!-- 開發模式下使用,可以打印出更詳細的錯誤信息 --> <constant name="struts.devMode" value="true"></constant> <!-- 文件上傳 臨時文件 --> <constant name="struts.multipart.saveDir" value="/tmp"/> <!-- 文件上傳 限制文件上傳大小 單位kb --> <constant name="struts.multipart.maxSize" value="900000000"/> <!-- 該 屬性指定需要Struts 2處理的請求後綴,該屬性的默認值是action,即 所有匹配*.action的請求都由Struts 2處理。如 果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開 --> <constant name="struts.action.extension" value="do"/> <constant name="struts.convention.default.parent.package" value="struts-default"/> <!-- 是否開啓動態方法調用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="test" extends="struts-default" namespace="/test"> <!-- 設置允許調用的方法,此處設置當前包下的action允許調用所有方法,若要支持動態訪問,必須添加此配置 --> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="*_*_*" class="cn.totoro.com.web.{1}Action" method="{2}" /> </package> </struts>
3. spring相關配置文件
spring 相關配置文件只需注意頭中改成4.0版本即可
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" default-autowire="byName"> <!-- 你自己的spring相關配置 --> </beans>
升級Struts2.5主要的注意事項就是配置文件中標紅的部分。
Filter :
web.xml中注意filter是org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterr而不是 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
Struts動態方法調用 :
開啓動態方法調用 <constant name="struts.enable.DynamicMethodInvocation" value="true" />
使用通配符方式配置允許調用所有方法<global-allowed-methods>regex:.*</global-allowed-methods>
(當然,如果你只有指定的幾個方法需要調用,也可以不用通配符的方式配置,可指定可調用的方法,多個方法用逗號‘,’隔開,配置如下
<global-allowed-methods>login,userInfo</global-allowed-methods> 或 <allowed-methods>login,userInfo</allowed-methods>
)
個人理解,如有理解錯誤之處,還望大神們指點。