spring集成struts2

Struts2前身是WebWork,核心并没有改变,其实就是把WebWork改名为struts2,与Struts1一点关系没有。

Struts2中通过ObjectFactory接口实现创建及获取Action实例,类似于Spring的IoC容器,所以Action实例可以由ObjectFactory实现来管理,因此集成Spring的关键点就是如何创建ObjectFactory实现来从Spring容器中获取相应的Action Bean。

Struts2提供一个默认的ObjectFactory接口实现StrutsSpringObjectFactory,该类用于根据Struts2配置文件中相应Bean信息从Spring 容器中获取相应的Action。

因此Struts2.x与Spring集成需要使用StrutsSpringObjectFactory类作为中介者。

接下来让我们首先让我们准备Struts2x所需要的jar

准备Struts2.x需要的jar包,到Struts官网http://struts.apache.org/下载struts-2.2.1.1版本,拷贝如下jar包到项目的lib目录下并添加到类路径:

 

libstruts2-core-2.2.1.1.jar              //核心struts2包

libxwork-core-2.2.1.1.jar              //命令框架包,独立于Web环境,为Struts2

//提供核心功能的支持包

libfreemarker-2.3.16.jar               //提供模板化UI标签及视图技术支持

libognl-3.0.jar                       //对象图导航工具包,类似于SpEL

lib struts2-spring-plugin-2.2.1.1.jar      //集成Spring的插件包

libcommons-logging-1.0.4.jar          //日志记录组件包(已有)

libcommons-fileupload-1.2.1.jar        //用于支持文件上传的包

 

10.3.2  使用ObjectFactory集成

1、Struts2.x的Action实现:

Java代码  收藏代码
  1.       
  2. package cn.javass.spring.chapter10.struts2x.action;  
  3. import org.apache.struts2.ServletActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. public class HelloWorldAction extends ActionSupport {  
  6.     private String message;  
  7.     @Override  
  8.     public String execute() throws Exception {  
  9.         ServletActionContext.getRequest().setAttribute("message", message);  
  10.         return "hello";  
  11.     }  
  12.     public void setMessage(String message) {//setter注入  
  13.         this.message = message;  
  14.     }  
  15. }  

2、JSP页面定义,使用Struts1x中定义的JSP页面“webapp/WEB-INF/jsp/hello.jsp”;

3、Spring一般配置文件定义(resources/chapter10/applicationContext-message.xml):

在此配置文件中定义我们使用的“message”Bean;

Java代码  收藏代码
  1. <bean id="message" class="java.lang.String">  
  2.     <constructor-arg index="0" value="Hello Spring"/>  
  3. </bean>  

4、Spring Action 配置文件定义(resources/chapter10/hello-servlet.xml):

Java代码  收藏代码
  1. <bean name="helloAction" class="cn.javass.spring.chapter10.struts2x.action.HelloWorldAction" scope="prototype">  
  2.     <property name="message" ref="message"/>  
  3. </bean>  

Struts2的Action在Spring中配置,而且应该是prototype,因为Struts2的Action是有状态的,定义在Spring中,那Struts如何找到该Action呢?

5、struts2配置文件定义(resources/chapter10/struts2x/struts.xml):

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>  
  7.     <constant name="struts.devMode" value="true"/>  
  8.     <package name="default" extends="struts-default">  
  9.         <action name="hello" class="helloAction">  
  10.             <result name="hello" >/WEB-INF/jsp/hello.jsp</result>  
  11.         </action>  
  12.     </package>  
  13. </struts>  
  • struts.objectFactory通过在Struts配置文件中使用常量属性struts.objectFactory来定义Struts将要使用的ObjectFactory实现,此处因为需要从Spring容器中获取Action对象,因此需要使用StrutsSpringObjectFactory来集成Spring;
  • <action name="hello" class="helloAction">:StrutsSpringObjectFactory对象工厂将根据<action>标签的class属性去Spring容器中查找同名的Action Bean;即本例中将到Spring容器中查找名为helloAction的Bean。

6、web.xml部署描述符文件定义(webapp/WEB-INF/web.xml):

6.1、由于Struts2只能使用通用配置,因此需要在通用配置中加入Spring Action配置文件(chapter10/struts2x/struts2x-servlet.xml):

Java代码  收藏代码
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.         classpath:chapter10/applicationContext-message.xml,  
  5.         classpath:chapter10/struts2x/struts2x-servlet.xml  
  6.     </param-value>  
  7. </context-param>  

       Struts2只能在通用配置中指定所有Spring配置文件,并没有如Struts1自己指定Spring配置文件的实现。

6.2、Strut2前端控制器定义,在web.xml中添加如下配置:

Java代码  收藏代码
  1. <!-- Struts2.x前端控制器配置开始   -->  
  2. <filter>  
  3.     <filter-name>struts2x</filter-name>  
  4.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  5.     <init-param>  
  6.               <param-name>config</param-name>  
  7.               <param-value>  
  8.                      struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml  
  9.                </param-value>  
  10.        </init-param>  
  11. </filter>  
  12. <filter-mapping>  
  13.     <filter-name>struts2x</filter-name>  
  14.     <url-pattern>*.action</url-pattern>  
  15. </filter-mapping>  
  16. <!-- Struts2.x前端控制器配置结束   -->  
  • FilterDispatcherStruts2前端控制器为FilterDispatcher,是Filter实现,不是Servlet;
  • config通过初始化参数config指定配置文件为struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml;如果不知道该参数则默认加载struts-default.xml,struts-plugin.xml,struts.xml(位于webapp/WEB-INF/classes下);显示指定时需要将struts-default.xml,struts-plugin.xml也添加上。
  • *.action将拦截以“.action”结尾的HTTP请求;
  • struts2xFilterDispatcher前端控制器的名字为struts2x,因此相应的Spring配置文件名为struts2x-servlet.xml。

7、执行测试,在Web浏览器中输入http://localhost:8080/hello.action可以看到“Hello Spring”信息说明Struts2集成成功。

 

集成Strut2也是非常简单,在此我们总结一下吧:

  • 配置文件位置:

         Struts配置文件默认加载“struts-default.xml,struts-plugin.xml, struts.xml”,其中struts-default.xml和struts-plugin.xml是Struts自带的,而struts.xml是我们指定的,默认位于webapp/WEB-INF/classes下;

         如果需要将配置文件放到其他位置,需要在web.xml的<filter>标签下,使用初始化参数config指定,如“struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml”,其中“struts-default.xml和struts-plugin.xml”是不可省略的,默认相对路径是类路径。

  • 集成关键ObjectFactory在Struts配置文件或属性文件中使用如下配置知道使用StrutsSpringObjectFactory来获取Action实例:

 在struts.xml中指定:

Java代码  收藏代码
  1. <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>  

或在struts.properties文件(webapp/WEB-INF/classes/)中:

java代码:
Java代码  收藏代码
  1. struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory  
  • 集成关键Action定义:

                StrutsSpringObjectFactory将根据Struts2配置文件中的<action class=””>标签的classes属性名字去到Spring配置文件中查找同名的Bean定义,这也是集成的关键。

 

  • Spring配置文件中Action定义:由于Struts2的Action是有状态的,因此应该将Bean定义为prototype。

 

如图10-5,Sturt2与Spring集成的关键就是StrutsSpringObjectFactory,注意图只是说明Struts与Spring如何通过中介者StrutsSpringObjectFactory来实现集成,不能代表实际的类交互。

原文地址:https://www.cnblogs.com/doudouxiaoye/p/5788263.html