重新整理struts2与spring整合相关知识

来源:互联网 发布:注册域名赚钱 编辑:程序博客网 时间:2024/06/04 18:02

1.开发环境与版本

JDK1.6.0_24+Eclipse4.2+Struts2.3.15.2+Spring3.2.0+tomcat7.0.42

2.整合前准备

需要准备的jar包有如下几个:

2.1 Struts2需要的jar包(即从struts2的lib中找出来的,因为部分jar包,spring和其他框架也在使用):

struts2-core-2.3.15.2.jar

xwork-core-2.3.15.2.jar

commons-fileupload-1.3.jar

commons-io-2.0.1.jar

commons-lang3-3.1.jar

commons-logging-1.1.3.jar

freemarker-2.3.19.jar

javassist-3.11.0.GA.jar

ognl-3.0.6.jar

struts2-spring-plugin-2.3.15.2.jar(此包在与spring整合时必不可少,有网上资料说可以不用此包,我在实际使用时发现没有此包是不行的,不知道是否为struts和spring版本的问题。)

2.2 Spring需要的jar包:

spring-asm-3.2.0.M1.jar

spring-beans-3.2.0.M1.jar

spring-context-3.2.0.M1.jar

spring-core-3.2.0.M1.jar

spring-expression-3.2.0.M1.jar

spring-web-3.2.0.M1.jar


3.我的配置

3.1 web.xml关于Struts2的配置

<!-- 整合struts2开始 -->    <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern>    </filter-mapping><!-- 整合struts2结束 -->

3.2 web.xml中关于spring的配置

<!-- 整合spring开始 -->    <!-- 指明spring配置文件在何处 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:applicationContext.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener><!-- 整合spring结束 -->

3.3 struts.xml中的配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 指定字符编码 --><constant name="struts.i18n.encoding" value="UTF-8"></constant><package name="struts2" extends="struts-default">          <action name="login" class="com.test.action.LoginAction">              <result name="success">/pages/result.jsp</result>              <result name="input">/pages/login.jsp</result>          </action>          </package>      </struts>

3.4 spring的applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><bean id="loginService" class="com.test.service.impl.LoginServiceImpl"></bean></beans>

4.一些疑问

4.1 在这里struts2的action配置与没有和spring整合之前一般无二,有的网上资料里说有了struts2-spring-plugin-2.3.15.2.jar包,struts2的action均由spring容器负责实例化;还有的网上资料采用了如下配置,在spring的applicationContext.xml中增加:

<bean id="loginAction" class="com.test.action.LoginAction">   <property name="loginService" ref="loginService"></property></bean> 

然后在struts.xml中将action的配置改为:

<package name="struts2" extends="struts-default">          <action name="Login" class="loginAction">              <result name="success">/pages/result.jsp</result>              <result name="input">/pages/login.jsp</result>          </action>  </package>

与spring的配置文件中applicationContext.xml中的bean id对应。但个人不太喜欢这种配置方法,仍按照struts的方式来配置,spring里bean只负责service的实例化。经测试,可以正常使用,很疑惑上面的配置有何好处。