struts2.5 从老版本升级到2.5版本

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>


个人理解,如有理解错误之处,还望大神们指点。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值