Spring-Struts2两大框架整合过程介绍(基于注解方式)

关于这两大框架的整合的好处最主要的就是在整合后,可以使用Spring的配置文件applicationContext.xml来描述以来关系,在Struts2的配置文件struts.xml来使用Spring创建bean,从而免去每次自己实例化对象。
下面就详细介绍下具体过程实现:

  1. 准备开发环境(导入依赖包)
    分别导入Spring和Struts必须包,在此不一一列举了,因为版本的不同包的数量也是有差异的。但是尤为注意的就是要导入commons-logging和struts2-spring-plugin包,否则启动时就会抛异常。
    在src下建立struts.xml、applicationContext.xml文件(建议拷贝),如果写的项目挺大建议新建一个名字为config的Source Folder,把配置文件放在里面。
    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>
为了是Spring容器启动时自动加载applicationContext配置,所以此时还要在web.xml中配置ContextLoaderListener监听器,主要原因就是他实现了ServletContextListener这个接口。具体代码如下:
<!-- 配置Sping的用于初始化ApplicationContext的监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
</context-param>

说明:默认情况下,会加载WEB-INF/applicationContext.xml这个文件,我们可以通过配置contextConfigLocation参数改变配置文件的路径,这里我选用更为方便的classpath属性作为工程文件的WEB-INF路径。

3.开发环境搭建好,现在就写测试文件包括配置文件(一下代码注意文件所在的包位置)
首先写一个Action:TestAction 代码如下:

package com.sun.action;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.service.UserService;

//添加注解使其到Spring容器中。
@Controller("testAction")
//设置多例
@Scope("prototype")
public class TestAction extends ActionSupport {

    @Resource
    private UserService userService;

    @Override
    public String execute() throws Exception {
        userService.saveUser(new Object());// 模拟一个操作
        System.out.println("===--===--->>TestAction.execute()");
        return SUCCESS;
    }
}

4.接着根据TestAction编写strurs2,applicationContext.xml配置文件 代码如下:

<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <!-- 通过常量方式改变struts默认的属性配置 -->
    <constant name="struts.action.extension" value="do"/>
    <!-- 设置开发模式,重新加载国际化资源文件和配置文件 -->
    <constant name="struts.devMode" value="true"/>

    <package name="default" namespace="/" extends="struts-default">
        <!-- 配置action,在没有与Spring整合时,class属性写的是类的全限定名 (包括包名)
            当与Spring整合后,class属性写的是bean的名称。
            注意:这个bean一定要是多例的模式(prototype)。
            class:要设置根据Action类名首字母改为小写
        -->
        <action name="test" class="testAction">
            <result name="success">/test.jsp</result>
        </action>
    </package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Middle tier application context definition for the image database.
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
        <!-- 自动扫描与装配bean -->
        <context:component-scan base-package="com.sun"></context:component-scan>
</beans>

5.编写测试类 代码如下:

package com.sun.test;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sun.listener.MyEvent;
import com.sun.service.UserService;

public class UserServiceTest {
    @Test
    public void testSaveUser() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ac.getBean("userService");
        System.out.println(userService);
    }
}

具体jsp文件在此不赘述了,那么量大框架整合完毕。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章