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

1、导入依赖包

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

2、web.xml的配置

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

  1. <filter>
  2. <filter-name>struts2</filter-name>
  3. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>struts2</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

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

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

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

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

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>WEB-INF/classes/applicationContext.xml</param-value>
  4. </context-param>

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

3、测试类

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

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

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

  1. public interface IocTestInterface {
  2. public List getList();
  3. }

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

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

  1. public class IocAction extends ActionSupport {
  2. private IocTestInterface iti;
  3. private List list;
  4.  
  5. public List getList() {
  6. return list;
  7. }
  8. public void setList(List list) {
  9. this.list = list;
  10. }
  11. public IocTestInterface getIti() {
  12. return iti;
  13. }
  14. public void setIti(IocTestInterface iti) {
  15. this.iti = iti;
  16. }
  17.  
  18. public String execute() throws Exception {
  19. this.setList(iti.getList());
  20. return super.execute();
  21. }
  22. }

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

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

  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3.  
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  5. <html>
  6.  
  7. <body>
  8. This is my JSP page. <br><br>
  9.  
  10. <s:iterator value="list" id="current">
  11. <li><s:property value="current"/></li>
  12. </s:iterator>
  13.  
  14. </body>
  15. </html>

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

  1. public class IocTestImpl implements IocTestInterface {
  2. public List getList() {
  3. List l = new ArrayList();
  4. l.add("abc");
  5. l.add("def");
  6. l.add("hig");
  7. return l;
  8. }
  9. }
  1. public class IocTest2Impl implements IocTestInterface {
  2. public List getList() {
  3. List l = new ArrayList();
  4. l.add("123");
  5. l.add("456");
  6. l.add("789");
  7. return l;
  8. }
  9. }

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

  1. <beans xmlns ="http://www.springframework.org/schema/beans"
  2. xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation ="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  5.  
  6. <bean name="IocAction" class="sy.struts2.ioc.IocAction">
  7. <property name="iti">
  8. <bean class="sy.struts2.ioc.IocTestImpl"></bean>
  9. </property>
  10. </bean>
  11.  
  12. </beans>

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

5、编写struts.xml配置文件

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

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

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

创建package并配置Action

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

6、发布并运行

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

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

  1. <bean name="IocAction" class="sy.struts2.ioc.IocAction">
  2. <property name="iti">
  3. <bean class="sy.struts2.ioc.IocTest2Impl"></bean>
  4. </property>
  5. </bean>

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

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

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

Struts2学习笔记——Struts2与Spring整合的更多相关文章

  1. Spring学习笔记六:Spring整合Hibernate

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6785323.html  前言:整合概述 Spring整合Hibernate主要是把Hibernate中常用的S ...

  2. Struts2学习笔记--Struts2的体系结构

    1. Struts2体系结构 Struts是以前端控制器框架为主体的框架,用户的请求会通过控制器选择不同的Action类来执行具体的操作,在Action类中所有的Servlet对象(request.r ...

  3. Struts2学习笔记——Struts2搭建和第一个小程序

    1.新建web项目 2.配置Struts2核心过滤器 (1)打开web.xml文件,做以下配置: <?xml version="1.0" encoding="UTF ...

  4. SpringBoot学习笔记二之Spring整合Mybatis

    原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...

  5. Struts2学习笔记⑧

    今天是Struts2学习笔记的最后一篇文章了.用什么做结尾呢,这两天其实还学了很多东西,没有记录下,今天就查漏补缺一下. 文件上传与下载.FreeMarker以及昨天没做完的例子 文件上传与下载 文件 ...

  6. Struts2 学习笔记(概述)

    Struts2 学习笔记 2015年3月7日11:02:55 MVC思想 Strust2的MVC对应关系如下: 在MVC三个模块当中,struts2对应关系如下: Model: 负责封装应用的状态,并 ...

  7. Struts2学习笔记①

    Struts2 学习笔记① 所有的程序学习都从Hello World开始,今天先跟着书做一个HW的示例. Struts2是一套MVC框架,使用起来非常方便,接触到现在觉得最麻烦的地方是配置文件.我的一 ...

  8. Struts2学习笔记NO.1------结合Hibernate完成查询商品类别简单案例(工具IDEA)

    Struts2学习笔记一结合Hibernate完成查询商品类别简单案例(工具IDEA) 1.jar包准备 Hibernate+Struts2 jar包 struts的jar比较多,可以从Struts官 ...

  9. Git学习笔记与IntelliJ IDEA整合

    Git学习笔记与IntelliJ IDEA整合 一.Git学习笔记(基于Github) 1.安装和配置Git 下载地址:http://git-scm.com/downloads Git简要使用说明:h ...

随机推荐

  1. device-pixel-radio

    移动web开发之像素和DPR 今天看到一个面试题,为iphone6s的自适应,答案是@media(min-device-width:414px) and(max-device-width:736px) ...

  2. Ext.util.Format.date 时间格式的设置与转换

    Ext.util.Format.date 如下这段简单的代码:  var d = new Date(value.time); var s = Ext.util.Format.date(d, 'Y-m- ...

  3. DEEP COMPRESSION小记

    2016ICLR最佳论文 Deep Compression: Compression Deep Neural Networks With Pruning, Trained Quantization A ...

  4. 整理OpenResty+Mysql+Tomcat+JFinal+Cannal+HUI

    阿里云运维主机 118.190.89.22 26611 1.CentOS6.9下安装OpenResty 2.CentOS6.9下安装MariaDB10.2.11 3.使用Intellij IDEA把J ...

  5. Java编程的逻辑 (22) - 代码的组织机制

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  6. hadoop2.5搭建过程

    1 搭建环境所使用的资源 VMware Workstation 9 ubuntu-14.04.2-desktop-amd64.iso jdk-7u80-linux-x64.tar.gz hadoop- ...

  7. 【BZOJ】4292: [PA2015]Równanie

    题解 \(f(n)\)的取值范围最多\(9^2 * 18\) 直接枚举判断就好 代码 #include <bits/stdc++.h> #define fi first #define s ...

  8. 【AtCoder】ARC100 题解

    C - Linear Approximation 找出\(A_i - i\)的中位数作为\(b\)即可 题解 #include <iostream> #include <cstrin ...

  9. matplotlib显示中文异常处理

    matplotlib显示中文 [做个记录,方便以后使用] [一般导入方式] import matplotlib.pyplot as plt [效果图] [方式一]FontProperties impo ...

  10. BeagleBone Black教程之BeagleBone Black使用到的Linux基础

    BeagleBone Black教程之BeagleBone Black使用到的Linux基础 BeagleBone Black涉及到的Linux基础 在许多没有Linux相关经验的人看来,Linux看 ...