一、基本支持

通常我们整合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>

ContextLoaderListener监听器通过查找 web 应用初始化参数 contextConfigLocation 来获取 Bean 配置文件的位置。如果有多个 Bean 配置文件, 可以通过逗号或空格进行分隔。contextConfigLocation 的默认值为 /WEB-INF/applicationContext.xml。若实际的文件和默认值一致则可以省略这个 web 应用的初始化参数。





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 实例。

Spring整合Struts2的方法的更多相关文章

  1. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  2. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  3. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  4. Spring框架学习(5)spring整合struts2

    内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...

  5. 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action

    SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib  开发基本包 Struts2有一 ...

  6. spring整合struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...

  7. Spring 整合 Struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...

  8. Maven环境下搭建SSH框架之Spring整合Struts2

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Struts2:2.5.10 Spring:4.3.8.RELEASE 注意:其他版本在某些特性的使用上可能稍微存在差别 2.准备工作 ...

  9. Spring整合Struts2,Hibernate的xml方式

    作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...

随机推荐

  1. 【CF645D】 Robot Rapping Results Report(拓扑排序,二分)

    题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序, 若没有唯一拓扑序输出-1 思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<&g ...

  2. Oracle Dual 表详解

    1.DUAL表的用途Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中--查看当前连接用户SQL> select user from dua ...

  3. 【Visual Studio】让用VS2012/VS2013编写的程序在XP中顺利运行(转)

    原文转自 http://blog.csdn.net/asanscape/article/details/38752655 微软为了推销自家平台,默认配置下VS2012和VS2013编写的应用程序只能在 ...

  4. Ajax的post方式提交数据

    最新需要学习如何使用 POST 提交方法的接口,正好看到了Ajax 版本的感觉不错分享给大家,欢迎高手指点. <SCRIPT LANGUAGE=”javascript”> <!– f ...

  5. [ASP.NET Core] Tips

    让Cache支持SetObject using Microsoft.AspNetCore.Http; using Newtonsoft.Json; public static class Sessio ...

  6. Codeforces 934 A.Compatible Pair

    http://codeforces.com/contest/934 A. A Compatible Pair   time limit per test 1 second memory limit p ...

  7. Codechef Eugene and big number(矩阵快速幂)

    题目链接 Eugene and big number 题目转化为 $f(n) = m * f(n - 1) + a$ $f(n + 1) = m * f(n) + a$ 两式相减得 $f(n + 1) ...

  8. python使用Queue进行进程间通信

    1.Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信. 可以使用multiprocessing模块的Queue实现多进程之间的数据传递, Queue本身是一个消息列队程序: f ...

  9. AtCoder - 2581 Meaningful Mean

    Problem Statement You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K. ...

  10. MySQL中数据类型(char(n)、varchar(n)、nchar(n)、nvarchar(n)的区别)(转)

    一.第一种 char(n)和varchar(n)的区别: 在这里我们可以清楚的看到他们表面的区别就是前面是否有var,在这里解释一下var是什么意思,var代表“可变的”的意思 下面看个例子: )// ...