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整合的更多相关文章
- Spring学习笔记六:Spring整合Hibernate
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6785323.html 前言:整合概述 Spring整合Hibernate主要是把Hibernate中常用的S ...
- Struts2学习笔记--Struts2的体系结构
1. Struts2体系结构 Struts是以前端控制器框架为主体的框架,用户的请求会通过控制器选择不同的Action类来执行具体的操作,在Action类中所有的Servlet对象(request.r ...
- Struts2学习笔记——Struts2搭建和第一个小程序
1.新建web项目 2.配置Struts2核心过滤器 (1)打开web.xml文件,做以下配置: <?xml version="1.0" encoding="UTF ...
- SpringBoot学习笔记二之Spring整合Mybatis
原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...
- Struts2学习笔记⑧
今天是Struts2学习笔记的最后一篇文章了.用什么做结尾呢,这两天其实还学了很多东西,没有记录下,今天就查漏补缺一下. 文件上传与下载.FreeMarker以及昨天没做完的例子 文件上传与下载 文件 ...
- Struts2 学习笔记(概述)
Struts2 学习笔记 2015年3月7日11:02:55 MVC思想 Strust2的MVC对应关系如下: 在MVC三个模块当中,struts2对应关系如下: Model: 负责封装应用的状态,并 ...
- Struts2学习笔记①
Struts2 学习笔记① 所有的程序学习都从Hello World开始,今天先跟着书做一个HW的示例. Struts2是一套MVC框架,使用起来非常方便,接触到现在觉得最麻烦的地方是配置文件.我的一 ...
- Struts2学习笔记NO.1------结合Hibernate完成查询商品类别简单案例(工具IDEA)
Struts2学习笔记一结合Hibernate完成查询商品类别简单案例(工具IDEA) 1.jar包准备 Hibernate+Struts2 jar包 struts的jar比较多,可以从Struts官 ...
- Git学习笔记与IntelliJ IDEA整合
Git学习笔记与IntelliJ IDEA整合 一.Git学习笔记(基于Github) 1.安装和配置Git 下载地址:http://git-scm.com/downloads Git简要使用说明:h ...
随机推荐
- Python 单例模式讲解
Python 单例模式讲解 本节内容: classmethod用途 单例模式方法一 类__new__方法讲解 单例模式方法二 前言: 使用单例方法的好处:对于一个类,多次实例化会产生多个对象,若使用单 ...
- Error updating database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String异常处理
问题原因:Mybatis中对于时间参数进行比较时的一个BUG. 如果拿传入的时间类型参数与空字符串‘‘进行对比判断则会引发异常.,所以应该去掉该判断, 只保留非空判断就正常了 <if test= ...
- laravel的启动过程解析
laravel的启动过程,也是laravel的核心,对这个过程有一个了解,有助于得心应手的使用框架,希望能对大家有点帮助. 统一入口 laravel框架使用了统一入口,入口文件:/public/ind ...
- Centos7下yum安装zabbix-server的部署(一)
一.环境准备 OS:CentOS 7.2 64bit Zabbix版本:3.0.12 MySQL版本:5.6 hostname ip 主机用途zabbix-server 10.0.0.44 服务端 z ...
- 利用yum安装时,报错 [Errno 256] No more mirrors to try.
问题: [root@gg ~]# yum install -y perl-DBD-MySQL Loaded plugins: product-id, refresh-packagekit, secu ...
- 【Java】 子字符串的比较(substring的==与equal()使用)
public class Test { public static void main(String[] args) { String str1="good"; System.ou ...
- 【转】HTML5 API——无刷新更新地址 history.pushState/replaceState 方法
(window.location)在通过JavaScript更改以后,浏览器都会通过刷新来到达你更改后的URL(location的意思就是位 置..) 而在JavaScript MVC开始流行之后,通 ...
- 002.Rsync详细配置项
一 相关参数 全局参数 在文件中[module]之前的所有参数都是全局参数,当然也可以在全局参数部分定义模块参数,这时候该参数的值就是所有模块的默认值. port 指定后台程序使用的端口号,默认为87 ...
- 初识PHP(一)
做为一名合格的前端开发攻城狮,了解一门服务端语言是必须的,所以我选了php.都说学的第一门语言对第二门语言会产生较大的影响,确实,每当我看到一个php知识点时,就同时会想到这个知识点在Javascri ...
- 21.python中的闭包和装饰器
python中的闭包从表现形式上定义(解释)为:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure). 以下说明主要针对 python ...

