记录一次struts2转spring MVC的经历

应公司要求,需要对原有SSH(struts2+hibernate3+spring3)框架进行升级,升级后框架为springMVC4+spring4+hibernate4+spring session+shiro,下面简单介绍下升级过程。

1、修改pom.xml,这步不多说,按照需要配置对应版本即可,途中会遇到各种各样版本冲突问题,没办法难过,慢慢改吧,修改后的版本如下:


2、修改web.xml

    1)删除StrutsPrepareFilter,struts2IntegrationFilter过滤器

    2)添加RequestContextListener监听,用于springMVC中获取request对象:        

      <listener>    
	        <listener-class>    
	            org.springframework.web.context.request.RequestContextListener
	        </listener-class>    
	</listener>

    3)添加:  spring session、shiro过滤器

        <filter>
		<filter-name>springSessionRepositoryFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>springSessionRepositoryFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter>
		<filter-name>shiroFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>shiroFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
    4)添加springMVC servlet
      <servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

3、添加springMVC配置文件:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd" default-autowire="byName">
	<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
	<context:component-scan base-package="com.skysz.framework,com.skysz.app">  
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   
	</context:component-scan>
	<!-- 解除servlet对静态资源文件访问的限制,使得静态资源先经过 -->
	<mvc:default-servlet-handler/>
		
	<mvc:annotation-driven>
		<mvc:message-converters>
			<!-- 文件等二进制数据下载 -->
			<!-- json处理 -->
			<bean class="com.skysz.framework.springmvc4.config.MyFastJsonHttpMessageConverter">
				<description>JSON转换器</description>
				
				<property name="myCharset" value="UTF-8" />
				
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>  
		             	<value>text/html;charset=UTF-8</value>
					</list>
				</property>
			<!-- 
				<property name="filters">
					<bean id="HibernateLazyEntityFilter" class="com.skysz.framework.spring4.config.HibernateLazyEntityFilter"></bean>
				</property>
			 -->
				
				<property name="features">
	               <array>
	                   <value>QuoteFieldNames</value>
	                   <value>WriteDateUseDateFormat</value>
		               <!-- 字符类型字段如果为null,输出为"",而非null -->
		               <value>WriteNullStringAsEmpty</value>
		               <!-- 非字符串类型全转换为字符串,不能开启
		               <value>WriteNonStringValueAsString</value> -->
	               </array>
	           </property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>  
	
		
    <!-- baseMessageSource在spring配置文件中配置 -->
    <bean id="messageSource" parent="baseMessageSource" />
    
	
	<!-- 拦截器 -->
				<!-- 
	<mvc:interceptors>
		<mvc:interceptor>
			
		</mvc:interceptor>
	</mvc:interceptors>
				 -->
	<!-- AOP式方法级权限检查  -->
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
		<!--  depends-on="lifecycleBeanPostProcessor" >--> 
		<property name="proxyTargetClass" value="true" />
	</bean>
	<!-- 
	<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    	<property name="securityManager" ref="securityManager"/>
	</bean>
	 -->
	<mvc:view-resolvers>
	    <mvc:jsp cache-views="false" prefix="/" suffix=".jsp" />  
	</mvc:view-resolvers>
	
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<description>配置文件上传</description>
		<property name="defaultEncoding" value="UTF-8" />
		<!-- 文件大小最大值 -->
		<property name="maxUploadSize" value="10485760000" />
		<!-- 内存中的最大值 -->
		<property name="maxInMemorySize" value="40960" />
	</bean>
	<!-- 资源重定向(仅作为后台使用不提供静态资源) -->
	<mvc:resources mapping="/app/dhtmlx/**" location="/dhtmlx/"/>
	<mvc:resources mapping="/app/extjs/**" location="/extjs/"/>
	<mvc:resources mapping="/app/js/**" location="/js/"/>
	<mvc:resources mapping="/app/resource/**" location="/resource/"/>
	<mvc:resources mapping="/app/template/**" location="/template/"/>
	<!-- <mvc:default-servlet-handler />将静态资源的处理经由Spring MVC框架交回Web应用服务器处理。而<mvc:resources />更进一步,由Spring MVC框架自己处理静态资源,并添加一些有用的附加值功能。
		首先,<mvc:resources />允许静态资源放在任何地方,如WEB-INF目录下、类路径下等,你甚至可以将JavaScript等静态文件打到JAR包中。通过location属性指定静态资源的位置,
			由于location属性是Resources类型,因此可以使用诸如"classpath:"等的资源前缀指定资源位置。传统Web容器的静态资源只能放在Web容器的根路径下,<mvc:resources />完全打破了这个限制。
		其次,<mvc:resources />依据当前著名的Page Speed、YSlow等浏览器优化原则对静态资源提供优化。你可以通过cacheSeconds属性指定静态资源在浏览器端的缓存时间,
			一般可将该时间设置为一年,以充分利用浏览器端的缓存。在输出静态资源时,会根据配置设置好响应报文头的Expires 和 Cache-Control值。 
	-->
</beans>

4、添加springsession、shiro、redis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- SPRING-SESSION -->
	<!-- 这个 bean 中又会自动产生多个 bean ,用于相关操作,极大的简化了我们的配置项。其中有个比较重要的是 springSessionRepositoryFilter  -->
	<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="600" />
        <property name="redisNamespace" value="JZS" />
	</bean>
	<bean class="org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration">
		<property name="httpSessionListeners">
			<list>
				<bean class="com.skysz.framework.base.online.UserSessionListener" />
			</list>
		</property>
	</bean>
	 <!-- 设置Cookie domain 和 名称 -->
    <!---->
     <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
<!--         <property name="domainName" value=".example.com"/> -->
        <property name="cookieName" value="JSESSIONID_jz"/>
<!--         <property name="domainNamePattern" value="^.+?\\.(\\w+\\.[a-z]+)$"/> -->
    </bean> 
</beans>

redis配置文件不再列出。。。

5、删除相关struts配置文件

----------------------------------------------------------------------------------------------------------------------------------

以上配置文件就全部改造完成,以下是代码部分。

6、前端JSP页面需要将struts2自定义标签改掉,由于涉及到所有页面的改动,我的做法是重写struts2的标签,也就是说JSP页面不需要改动:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description><![CDATA[" custom i18n tag "]]></description>
  <display-name>custom i18n tag</display-name>
  <tlib-version>2.3</tlib-version>
  <short-name>s</short-name>
  <uri>/struts-tags</uri>

	<tag>
		<name>text</name>
		<tag-class>
			com.skysz.framework.easy.i18n.CustomSI18NTextTag
		</tag-class>
		<body-content>empty</body-content>
		<attribute>
			<name>defaultValue</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
	<tag>
		<name>i18n</name>
		<tag-class>
			com.skysz.framework.easy.i18n.CustomSI18NTag
		</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
</taglib>

7、修改action层代码,增加@RequestMapping("") @Controller等相关注解。

----------------------------------------------------------------------------------------------------------------------------------

以上就是需要修改的后台代码。

当然以上这只是大块的描述下流程,期间会遇到很多很多抓狂抓狂抓狂细节的问题。比如shiro权限控制,hibernate升级后方法不可用、监听失效等等等等等等等

随后有时间再跟大家一起分享具体的细节羡慕羡慕羡慕羡慕羡慕


  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值