一、基本支持

通常我们整合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. 58同城职位分类数据 json

    { "level0": {"0": "销售", "1": "客服", "2": ...

  2. 002如何升级 Linux 的内核?

    我们不应该升级 Linux 内核,而是始终使用 rpm 命令来安装新的内核,因为升级内核会让你的 Linux 机器处于一个无法启动的状态.

  3. CDOJ_327 BerOS file system

    原题地址:http://acm.uestc.edu.cn/#/problem/show/327 The new operating system BerOS has a nice feature. I ...

  4. Visual Studio各版本下载链接

    Visual Studio 2015 企业版 链接: https://pan.baidu.com/s/1NFGyIbm2RNsuwwEU-bYicQ 提取码: m961 Visual Studio 2 ...

  5. PHP微信公众平台OAuth2.0网页授权,获取用户信息代码类封装demo(二)

    一.这个文件微信授权使用的是OAuth2.0授权的方式.主要有以下简略步骤: 第一步:判断有没有code,有code去第三步,没有code去第二步 第二步:用户同意授权,获取code 第三步:通过co ...

  6. Xcode: This device is no longer connected error

    Quit the xcode and connect again will all right.

  7. 快速比较两个uiimage是否相等防止使用原始dsdata造成界面卡顿问题

    UIImage *imageLater = image1; UIImage *imagePre = image2; if (imageLater == imagePre){....}

  8. 创建Oracle数据库、数据库名与实例名与SID之间的关系(图文详解)

    分类: Oracle(9) 版权声明:转载请注明出处 JmilkFan_范桂飓:http://blog.csdn.net/jmilk 目录(?)[+] 目录 目录 软件环境 前言 安装Oracle监听 ...

  9. layer的alert、prompt等操作如何响应键盘的回车和ESC操作

    layer.prompt({title: '请输入数据', formType: 1, //隐藏用户输入内容 // 这个是确定按钮的事件 "success":function(){ ...

  10. Android Studio 设置项目Module编码,解决Android Studio项目执行时乱码问题

    Android Studio的项目设置逻辑与Eclipse有非常大的差别.运行的操作为File->Setting->File Encodings然后来进行设置,如图所看到的: waterm ...