如果不想看这些啰哩啰嗦的字,直接跳到最后,看那个整合的工具类,不过前提是你得知道不和spring整合,action是由谁创建的
一、 Struts 2框架整合Spring步骤
1、 复制文件。复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下。其中的x对应了Spring的版本号。还需要复制commons-logging.jar文件到WEB-INF/lib目录下。
2、 配置struts.objectFactory属性值。在struts.properties中设置struts.objectFactory属性值:struts.objectFactory = spring
或者在XML文件中进行常量配置:
<struts>
<constant name="struts.objectFactory" value="spring" />
...
</struts>
3、 配置Spring监听器。在web.xml文件中增加如下内容:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
4、 Spring配置文件。默认情况下,Spring配置文件为applicationContext.xml,该文件需要保存在Web应用的WEB-INF目录下。内容示例如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="personManager" class="com.acme.PersonManager"/>
</beans>
开发者实际上可以使用多个Spring配置文件,在web.xml中进行下列设置,从而使Spring的ApplicationContext通过匹配所给定模式的文件来初始化对象:
<!-- 用来定位Spring XML文件的上下文配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
</context-param>
5、 修改Struts配置文件。Struts 2框架整合Spring框架,需要在Struts配置文件中有所改变,下面是一个示例:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default">
<action name="foo" class="com.acme.Foo">
<result>foo.ftl</result>
</action>
</package>
<package name="secure" namespace="/secure" extends="default">
<action name="bar" class="bar">
<result>bar.ftl</result>
</action>
</package>
</struts>
该配置文件中定义了两个Action配置:foo是一个标准的Struts 2框架Action配置,指定了Action实现类为com.acme.Foo;bar对应的class并不存在,那么框架将在Spring配置文件中查找id属性为“bar”的定义,该配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="bar" class="com.my.BarClass" singleton="false"/>
...
</beans>
二、整合原理
Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的时候,它会用Struts2配置文件中的class属性去和Spring配置文件中的id属性进行关联,如果能找到,则由Spring创建,否则由Struts 2框架自身创建,然后由Spring来装配。Spring插件具体有如下几个作用:
— 允许Spring创建Action、Interceptror和Result。
— 由Struts创建的对象能够被Spring装配。
— 如果没有使用Spring ObjectFactory,提供了2个拦截器来自动装配action。
开发者不必在Spring中去注册action,尽管可以这么去做,通常Struts框架会自动地从action mapping中创建action对象
struts2-spring-plugin-x-x-x.jar插件中有一个struts-plugin.xml文件,该文件内容如下所示:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<!-- 设置Spring对象工厂为自动 -->
<constant name="struts.objectFactory" value="spring" />
<package name="spring-default">
<interceptors>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
</interceptors>
</package>
</struts>
其中设置了Struts 2框架常量struts.objectFactory的值为spring,实际上,spring是org.apache.struts2.spring.StrutsSpringObjectFactory类的缩写,默认情况下所有由Struts 2框架创建的对象都是由ObjectFactory实例化的,ObjectFactory提供了与其他IoC容器如Spring、Pico等集成的方法。覆盖这个ObjectFactory的类必须继承ObjectFactory类或者它的任何子类,并且要带有一个不带参数的构造方法。在这里用org.apache.struts2.spring.StrutsSpring ObjectFactory代替了默认的ObjectFactory。
如果Action不是使用Spring ObjectFactory创建的话,插件提供了两个拦截器来自动装配Action,默认情况下框架使用的自动装配策略是name,也就是说框架会去Spring中寻找与Action属性名字相同的bean,可选的装配策略还有:type、auto、constructor,开发者可以通过常量struts.objectFactory.spring.autoWire来进行设置。
Struts 2框架整合Spring后,处理用户请求的Action并不是Struts框架创建的,而是由Spring插件创建的。创建实例时,不是利用配置Action时指定的class属性值,根据bean的配置id属性,从Spring容器中获得相应的实例。
/* */ package org.apache.struts2.spring;
/* */
/* */ import com.opensymphony.xwork2.inject.Inject;
/* */ import com.opensymphony.xwork2.spring.SpringObjectFactory;
/* */ import javax.servlet.ServletContext;
/* */ import org.apache.commons.logging.Log;
/* */ import org.apache.commons.logging.LogFactory;
/* */ import org.springframework.context.ApplicationContext;
/* */ import org.springframework.web.context.support.WebApplicationContextUtils;
/* */
/* */ public class StrutsSpringObjectFactory extends SpringObjectFactory
/* */ {
/* 45 */ private static final Log log = LogFactory.getLog(StrutsSpringObjectFactory.class);
/* */
/* */ @Inject
/* */ public StrutsSpringObjectFactory(@Inject(value="struts.objectFactory.spring.autoWire", required=false) String autoWire, @Inject(value="struts.objectFactory.spring.useClassCache", required=false) String useClassCacheStr, @Inject ServletContext servletContext)
/* */ {
/* 54 */ boolean useClassCache = "true".equals(useClassCacheStr);
/* 55 */ log.info("Initializing Struts-Spring integration...");
/* */
/* 57 */ ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);</span>
/* 58 */ if (appContext == null)
/* */ {
/* 60 */ String message = "********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********\nLooks like the Spring listener was not configured for your web app! \nNothing will work until WebApplicationContextUtils returns a valid ApplicationContext.\nYou might need to add the following to web.xml: \n <listener>\n <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n </listener>";
/* */
/* 67 */ log.fatal(message);
/* 68 */ return;
/* */ }
/* */
/* 71 */ setApplicationContext(appContext);
/* */
/* 73 */ int type = 1;
/* 74 */ if ("name".equals(autoWire))
/* 75 */ type = 1;
/* 76 */ else if ("type".equals(autoWire))
/* 77 */ type = 2;
/* 78 */ else if ("auto".equals(autoWire))
/* 79 */ type = 4;
/* 80 */ else if ("constructor".equals(autoWire)) {
/* 81 */ type = 3;
/* */ }
/* 83 */ setAutowireStrategy(type);
/* */
/* 85 */ setUseClassCache(useClassCache);
/* */
/* 87 */ log.info("... initialized Struts-Spring integration successfully");
/* */ }
/* */ }
在具体点。贴上struts的工作原理
struts2并不是一个陌生的web框架,它是以Webwork的设计思想为核心,吸收struts1的优点,可以说
struts2是struts1和Webwork结合的产物。
struts2 的工作原理图:
一个请求在Struts2框架中的处理分为以下几个步骤:
1.客户端发出一个指向servlet容器的请求(tomcat);
2.这个请求会经过图中的几个过滤器,最后会到达FilterDispatcher过滤器。
3.过滤器FilterDispatcher是struts2框架的心脏,在处理用户请求时,它和请求一起相互配合访问struts2
的底层框架结构。在web容器启动时,struts2框架会自动加载配置文件里相关参数,并转换成相应的类。
如:ConfigurationManager、ActionMapper和ObjectFactory。ConfigurationManager 存有配置文件的一
些基本信息,ActionMapper存有action的配置信息。在请求过程中所有的对象(Action,Results,
Interceptors,等)都是通过ObjectFactory来创建的。过滤器会通过询问ActionMapper类来查找请求中
需要用到的Action。
4.如果找到需要调用的Action,过滤器会把请求的处理交给ActionProxy。ActionProxy为Action的代理对象
。ActionProxy通过ConfigurationManager询问框架的配置文件,找到需要调用的Action类。
5.ActionProxy创建一个ActionInvocation的实例。ActionInvocation在ActionProxy层之下,它表示了
Action的执行状态,或者说它控制的Action的执行步骤。它持有Action实例和所有的Interceptor。
6.ActionInvocation实例使用命名模式来调用,1. ActionInvocation初始化时,根据配置,加载Action相
关的所有Interceptor。2. 通过ActionInvocation.invoke方法调用Action实现时,执行Interceptor。在
调用Action的过程前后,涉及到相关拦截器(intercepetor)的调用。
7. 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果
通常是(但不总是,也可能是另外的一个Action链)一个需要被表示的JSP或者FreeMarker的模版。在表
示的过程中可以使用Struts2 框架中继承的标签。