通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器。也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中。
为此Spring提供了相应的监听器。通过注册 Servlet 监听器 ContextLoaderListener, Web 应用程序可以加载 Spring 的ApplicationContext 对象。这个监听器会将加载好的ApplicationContext 对象保存到 Web 应用程序的 ServletContext 中。随后, Servlet 或可以访问 ServletContext 的任意对象就能通过一个辅助方法来访问 Spring 的应用程序上下文了。
二、整合步骤(这里只做了最简单的演示,意在表述Spring管理Action的方法)
1、编写服务类和Action类
PersonService.java package com.kang.services; public class PersonService { public void save(){ System.out.println("PersonService's save...."); } } PersonAction.java package com.kang.actions; import com.kang.services.PersonService; public class PersonAction { private PersonService personService; public void setPersonService(PersonService personService) { this.personService = personService; } public String execute(){ System.out.println("execute...."); personService.save(); return "success"; } }
2、在Spring的配置文件中加入struts2的Action配置
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置服务类的bean --> <bean id="personService" class="com.kang.services.PersonService"></bean> <!-- 配置Action的bean。注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype --> <bean id="personAction" class="com.kang.actions.PersonAction" scope="prototype"> <property name="personService" ref="personService"></property> </bean> </beans>
spring 默认scope 是单例模式,这样只会创建一个Action对象,每次访问都是同一个Action对象,数据不安全。
struts2 是要求每次次访问都对应不同的Action,scope="prototype" 可以保证当有请求的时候都创建一个Action对象。
3、在struts2的配置文件中配置Action
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <!-- Spring 整合 Struts2 时, 在 Struts2 中配置的action的class需要指向IOC 容器中该 bean 的 id,即在Spring配置文件中配置的相应id--> <action name="person-save" class="personAction"> <result>/success.jsp</result> </action> </package> </struts>
4、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Spring7</display-name> <!-- 配置 Spring 配置文件的名称和位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 启动 IOC 容器的 ServletContextListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置 Struts2 的 Filter --> <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> </web-app>
5、编写jsp文件
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="person-save">Person Save</a> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>Success Page</h4> </body> </html>
6、将程序发布到tomcat上,访问index页面,点击超链接,可以跳转到success.jsp,则整合成功。
三、Spring整合struts2的两种基本策略
1、将 Action 实例交给 Spring 容器来负责生成, 管理。通过这种方式, 可以充分利用 Spring 容器的 IOC 特性, 提供最好的解耦。
整合流程:
---安装 Spring 插件: 把 struts2-spring-plugin-2.2.1.jar 复制到当前 WEB 应用的 WEB-INF/lib 目录下。
---在 Spring 的配置文件中配置 Struts2 的 Action 实例。
---在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID。
2、利用Spring插件的自动装配功能。当 Spring 插件创建 Action 实例后, 立即将 Spring 容器中对应的业务逻辑组件注入 Action 实例。
配置自动装配策略: Spring 插件的自动装配可以通过 struts.objectFactory.spring.autoWire 常量指定, 该常量可以接受如下值:
name: 根据属性名自动装配。
type: 根据类型自动装配。 若有多个 type 相同的 Bean, 就抛出一个致命异常; 若没有匹配的 Bean, 则什么都不会发生, 属性不会被设置。
auto: Spring 插件会自动检测需要使用哪种方式自动装配方式。
constructor: 同 type 类似, 区别是 constructor 使用构造器来构造注入所需的参数。
整合流程:
---安装 Spring 插件
---正常编写 struts 配置文件
---编写 spring 配置文件, 在该配置文件中不需要配置 Action 实例。