一、基本支持

通常我们整合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. CodeForces 97D. Robot in Basement

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. Linux--内核Uevent事件机制 与 Input子系统【转】

    转自:http://blog.csdn.net/lxl584685501/article/details/46379453 [-] 一Uevent机制 Uevent在kernel中的位置 Uevent ...

  3. Emmet插件的快捷键

    Emmet插件的快捷键 html:5+tab键,可以生成html标签.!+tab键,也可以生成html标签.============================================== ...

  4. C、C++变量auto,static,register,extern类型

    auto: 推导类型变量:编译器选项指示编译器如何使用 auto 关键字来声明变量. 如果指定默认选项 /Zc:auto,编译器从其初始化表达式中推导声明的变量的类型. 如果指定 /Zc:auto-, ...

  5. SetProcessWorkingSetSize 和内存释放

    http://hi.baidu.com/taobaoshoping/item/07410c4b6d6d9d0d6dc2f084 在应用程序中,往往为了释放内存等,使用一些函数,其实,对于内存操作函数要 ...

  6. repeated-substring-pattern

    https://leetcode.com/problems/repeated-substring-pattern/ 下面这个方法,开始我觉得挺好.可惜还是超时了.后来我就加了一个剪枝策略,只有长度能够 ...

  7. Linux 网卡驱动学习(二)(网络驱动接口小结)

    [摘要]前文我们分析了一个虚拟硬件的网络驱动例子,从中我们看到了网络设备的一些接口,其实网络设备驱动和块设备驱动的功能比较类似,都是发送和接收数据包(数据请求).当然它们实际是有很多不同的. 1.引言 ...

  8. jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径

    遇到的问题是这种,在jsp网页中嵌入了本地的图片,由于会用到上传到服务器的图片,所以没有放到项目里面,而是把全部图片单独放到一个文件夹里,然后打算使用绝对路径把要显示的图片显示出来.比方是放在了E盘的 ...

  9. 控制显示input隐藏和查看密码

    通过更改input的password和text类型即可实现 //点击函数,获取dom,判断更改属性. show(){ let input=document.getElementById("i ...

  10. Codeforces Beta Round #1 A. Theatre Square

    从今天開始.就要在Codeforces里有一个新的開始了,貌似任务非常重的说~~ Codeforces专题我将会记录全部通过的题目,事实上仅仅要通过的题目都是水题啊!. 题目大意: 依照要求计算须要多 ...