当前位置: 首页 > 文档资料 > 跟我学 Spring3 >

【第十章】集成其它Web框架 之 10.3 集成Struts2.x

优质
小牛编辑
111浏览
2023-12-01

先进行通用配置, 【第十章】集成其它Web框架 之 10.1 概述

10.3 集成Struts2.x

10.3.1 概述

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目录下并添加到类路径:

  • lib\struts2-core-2.2.1.1.jar //核心struts2包
  • lib\xwork-core-2.2.1.1.jar //命令框架包,独立于Web环境,为Struts2
  • //提供核心功能的支持包
  • lib\freemarker-2.3.16.jar //提供模板化UI标签及视图技术支持
  • lib\ognl-3.0.jar //对象图导航工具包,类似于SpEL
  • lib\ struts2-spring-plugin-2.2.1.1.jar //集成Spring的插件包
  • lib\commons-logging-1.0.4.jar //日志记录组件包(已有)
  • lib\commons-fileupload-1.2.1.jar //用于支持文件上传的包

10.3.2 使用ObjectFactory集成

1、Struts2.x的Action实现:


package cn.javass.spring.chapter10.struts2x.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
    private String message;
    @Override
    public String execute() throws Exception {
        ServletActionContext.getRequest().setAttribute("message", message);
        return "hello";
    }
    public void setMessage(String message) {//setter注入
        this.message = message;
    }
}

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

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

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

<bean id="message" class="java.lang.String">
    <constructor-arg index="0" value="Hello Spring"/>
</bean>

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

<bean name="helloAction" class="cn.javass.spring.chapter10.struts2x.action.HelloWorldAction" scope="prototype">
    <property name="message" ref="message"/>
</bean>

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

5、struts2配置文件定义(resources/chapter10/struts2x/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.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
    <constant name="struts.devMode" value="true"/>
    <package name="default" extends="struts-default">
        <action name="hello" class="helloAction">
            <result name="hello" >/WEB-INF/jsp/hello.jsp</result>
        </action>
    </package>
</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):

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:chapter10/applicationContext-message.xml,
        classpath:chapter10/struts2x/struts2x-servlet.xml
    </param-value>
</context-param>

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

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

<!-- Struts2.x前端控制器配置开始   -->
<filter>
    <filter-name>struts2x</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    <init-param>
              <param-name>config</param-name>
              <param-value>
                     struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml
               </param-value>
       </init-param>
</filter>
<filter-mapping>
    <filter-name>struts2x</filter-name>
    <url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- Struts2.x前端控制器配置结束   -->
  • FilterDispatcher:Struts2前端控制器为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请求;
  • struts2x:FilterDispatcher前端控制器的名字为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中指定:

<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

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

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来实现集成,不能代表实际的类交互。

图10-5 Strut2与Spring集成

原创内容,转载请注明出处【http://sishuok.com/forum/blogPost/list/0/2512.html