Struts2学习笔记——Struts2与Spring整合
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的“控制反转”
Struts2学习笔记——Struts2与Spring整合相关推荐
- SSM学习笔记4(Spring整合Mybatis,P26-P28,真吉尔难)
大概意思是先配置下替换下配置mapper那些XML的方式 核心对象一通我也听不懂的分析,分析出来是SqlSessionFactory.也不知道咋来的,先记下. 加MAVEN坐标忽略 配置SpringC ...
- Struts2学习笔记——Struts2概览
1. Struts2 和Struts1.X有什么区别?它们各自的发展轨迹又如何? (1)Struts2来自于Webwork2,并且与Struts1.X完全不兼容. (2)Apache的Struts1. ...
- 【Spring学习笔记 九】Spring声明式事务管理实现机制
什么是事务?事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用,关乎数据准确性的地方我们一定要用到事务,防止业务逻辑出错. 什么是事务管理,事务管理对于企业应用而言至 ...
- Git学习笔记与IntelliJ IDEA整合
2019独角兽企业重金招聘Python工程师标准>>> Git学习笔记与IntelliJ IDEA整合 一.Git学习笔记(基于Github) 1.安装和配置Git 下载地址:htt ...
- Git学习笔记与IntelliJ IDEA整合
Git学习笔记与IntelliJ IDEA整合 一.Git学习笔记(基于Github) 1.安装和配置Git 下载地址:http://git-scm.com/downloads Git简要使用说明:h ...
- Struts2学习笔记(一)--入门常见配置
Struts2框架的学习路线 l 第一天:Struts2的概述.Struts2的入门.Struts2常见的配置.Struts2的Action的编写 l 第二天:Struts2的数据的封装.结果页面配 ...
- Struts2学习笔记总结
**技术分析之Struts2框架的概述** 1. 什么是Struts2的框架 * Struts2是Struts1的下一代产品,是在 struts1和WebWork的技术基础上进行了合并的全新的Stru ...
- Spring Boot基础学习笔记18:Spring Boot整合Redis缓存实现
文章目录 零.学习目标 一.Spring Boot支持的缓存组件 二.基于注解的Redis缓存实现 (一)安装与启动Redis (二)创建Spring Boot项目 - RedisCacheDemo0 ...
- Struts2学习笔记(十七) 文件下载(File Download)
前面我们刚刚学完文件上传,那么我们就接着来看和文件上传相对应的文件下载吧.对于文件上传而言,文件下载实现起来要简单的多.通常我们可以直接将一个超链接的地址指向我们想要给用户下载的资源即可.但是如果这些 ...
最新文章
- ubuntu 14.04 安装 vmware 10 X64 后无法启动解决方法
- 皮一皮:好一道举世佳酿“青山卧雪龙”...
- C语言基础排序算法-冒泡排序
- Java反射在JVM的实现
- AspNetCore 多环境配置 以及注册 消费Consul
- Xamarin效果第二篇之公众号App
- python多线程编程(8):线程的合并和后台线程
- 度盘转存工具 v1.8
- 【IntelliJ】IntelliJ IDEA的安装破解及使用
- js页面传值php页面,不同页面,php如何js传值?
- Python模块(二)(序列化)
- 【20G】三菱PLC全套资料(手册+视频教程+编程软件+仿真软件)
- 西威变频器avo下载调试资料_西门子变频器使用BOP-2 面板调试 G120
- 【机器视觉】线阵相机模型说明以及使用HALCON标定助手对线阵相机进行标定
- restTemplate接收image/jpeg格式
- 以“掌上东航”为例,论混合开发在企业级项目中的实践
- 《SolidWorks 2014中文版完全自学手册》——第1章 SolidWorks 2014入门 1.1 SolidWorks的设计思想...
- 第六章 更多监督训练
- 测试 必用 工具(测试工具知多少)
- autojs联众识图
热门文章
- hdu1686 最大匹配次数 KMP
- C语言经典例82-八进制转换为十进制
- 【Android 逆向】Android 进程代码注入原理 ( 注入本质 | 静态注入和动态注入 | 静态注入两种方式 | 修改动态库重打包 | 修改 /data/app/xx/libs 动态库 )
- 【Android CPU 优化】Android CPU 调优 ( Trace 文件分析 | Android Profiler 工具 | CPU Profiler 工具 )
- 添加python虚拟环境
- 【《Objective-C基础教程 》笔记】(八)OC的基本事实和OC杂七杂八的疑问
- 病毒木马防御与分析实战
- python之运算符
- Swoole练习 Web
- ActiveX中添加对话框并显示