Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的bean。

1、导入依赖包

除了导入Struts2和Spring的核心库之外,还要导入commons-logging和struts2-spring-plugin包,否则启动会出异常

2、web.xml的配置

既然有Struts2,核心拦截器的配置是不可少的

<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>

通过配置ContextLoaderListener监听器,使容器启动时,自动加载applicationContext配置,

因为它实现了ServletContextListener这个接口,容器启动时会自动执行它实现的方法。

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

默认情况下,会加载WEB-INF/applicationContext.xml这个文件,我们可以通过配置contextConfigLocation参数改变配置文件的路径

<context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

以上配置均在web.xml文件的<web-app></web-app>区域

3、测试类

在浏览器请求一个Action方法,在Action方法内向一个对象请求一个List,然后转到index.jsp页面,在页面中输出Action请求到的List。

通过Spring依赖配置,控制Action请求的对象。

首先要编写一个接口,Action方法依赖这个接口,通过调用接口中的方法获取List

public interface IocTestInterface {public List getList();
}

下面编写Action类,这个类继承ActionSupport类,并覆盖其中的execute方法,

execute方法执行时,调用实现了上述接口对象的getList方法获取List

public class IocAction extends ActionSupport {private IocTestInterface iti;private List list;public List getList() {return list;}public void setList(List list) {this.list = list;}public IocTestInterface getIti() {return iti;}public void setIti(IocTestInterface iti) {this.iti = iti;}public String execute() throws Exception {this.setList(iti.getList());return super.execute();}
}

编写用来显示运行结果的jsp文件

遍历list,并将每个元素作为一行来显示

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><body>This is my JSP page. <br><br><s:iterator value="list" id="current"><li><s:property value="current"/></li></s:iterator></body>
</html>

系统的结构就是这样。下面编写两个实现IocTestInterface接口的类,用来提供数据

public class IocTestImpl implements IocTestInterface {public List getList() {List l = new ArrayList();l.add("abc");l.add("def");l.add("hig");return l;}
}

public class IocTest2Impl implements IocTestInterface {public List getList() {List l = new ArrayList();l.add("123");l.add("456");l.add("789");return l;}
}

4、编写applicationContext.xml配置依赖关系

<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-2.0.xsd"><bean name="IocAction" class="sy.struts2.ioc.IocAction"><property name="iti"><bean class="sy.struts2.ioc.IocTestImpl"></bean></property></bean></beans>

文件配置了id为IocAction的bean,路径为刚刚编写的Action类的路径,其中iti对象(请求数据的对象)配置为IocTestImpl(这里使用了匿名bean)

5、编写struts.xml配置文件

首先要告知Struts 2运行时使用Spring来创建对象

在<struts></struts>区域加入以下配置

<constant name="struts.objectFactory" value="spring" />

创建package并配置Action

<package name="hs" extends="struts-default"><action name="ioc" class="IocAction"><result>/index.jsp</result></action>
</package>

6、发布并运行

发布后启动Tomcat,用浏览器打开地址http://localhost:8080/StrutsIoc/ioc.action,获得了下面的页面

修改spring配置文件applicationContext.xml中配置

<bean name="IocAction" class="sy.struts2.ioc.IocAction"><property name="iti"><bean class="sy.struts2.ioc.IocTest2Impl"></bean></property>
</bean>

只是将注入到IocAction中的IocTestImpl修改为IocTest2Impl,也就是使用了另一个实现了IocTestInterface接口的类

重启服务器,再次打开刚才的地址

这也就是spring的“控制反转”

转载于:https://www.cnblogs.com/hoobey/p/6501139.html

Struts2与Spring整合相关推荐

  1. Struts2+Hibernate+Spring 整合示例

    转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...

  2. Struts2学习笔记——Struts2与Spring整合

    Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的be ...

  3. 二十六:Struts2 和 spring整合

    二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...

  4. Struts2 Hibernate Spring 整合的基本流程和步骤及其相关配置细节

    配置Hibernate环境 1. 把Hibernate的相关jar包复制到lib目录下: ① HIBERNATE_HOME/lib 下的相关的依赖的第三方包 ② HIBERNATE_HOME/hibe ...

  5. 【Structs2】struts2单例多例以及spring整合的问题

    单独的Struts2框架 1.struts2的Action默认是多实例的并非单例,也就是每次请求产生一个Action的对象,即每次访问的参数都被封装在Action的成员变量中. 2.struts2中A ...

  6. spring整合大全

    使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Strut ...

  7. Spring整合Struts2

    ①导入Struts2 jar包 ②在web.xml文件中创建过滤器 <?xml version="1.0" encoding="UTF-8"?> & ...

  8. Struts2、Hibernate、Spring整合所需要的jar包

    Struts2.Hibernate.Spring整合所需要的包: Struts2: struts2-core-2.0.14.jar  -- Struts2的核心包. commons-logging-1 ...

  9. struts2+hibernate-jpa+Spring+maven 整合(1)

    1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...

最新文章

  1. SAP MM 影响Vendor主数据维护界面的字段选择的四大因素?
  2. 晶振噪声及杂散_如何判断是否需要使用分立式晶振和振荡器呢?
  3. python ffmpeg pyav
  4. linux /dev/null 21,/dev/null 21 21 与 的意思
  5. html 书架样式css,CSS3 响应式书架布局
  6. 用服务器控件在后台调用前台客户端JS方法
  7. Element UI——布局容器el-container子元素强制上下排列
  8. 小店怎么做内容营销?这个家居店铺有诀窍
  9. WPF中TreeView.BringIntoView方法的替代方案
  10. java解析c语言的结构体,JAVA中如何实现C中的结构体数组的功能?
  11. 如何优雅地本地化构建Mybatis源码
  12. uinty粒子系统子物体变大_新Unity 最新粒子系统如何用代码改变参数
  13. c语言中 dr指令应用,在计算机进行程序存储及控制中,pc.ir.ar和dr各起什么作用?能否相互代替?...
  14. postman生成python代码_python代码直接生成可执行exe文件
  15. 前后端整合---请求
  16. 我一个软件测试,为什么要转开发?
  17. python正则表达式之match,search,findall区别
  18. 托管银行数字化转型发展探讨|专家视角
  19. javaSE基础篇之char
  20. 使用UIImageView实现加载GIF图片

热门文章

  1. x86分页机制——《x86汇编语言:从实模式到保护模式》读书笔记42
  2. 【不会吧不会吧,不会有人真的三分钟入门Python了吧?】Python编程基础
  3. Jackson 注解 -- 指定输出顺序
  4. Android Apk增量更新
  5. python爬虫从入门到放弃(一)之初识爬虫
  6. Binder实用指南(二) - 实战篇
  7. 农民斗地主——Binder fuzz安全研究
  8. Android进程间通信(IPC)机制Binder简要介绍和学习计划
  9. Arm64中的异常处理
  10. 【区块链基础知识系列】 第6课 区块链之分片技术(sharding)-区块链扩容问题的良方