Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整合起来,并且实现了action获取service,说明spring与struts2框架已经建立联系,互通了,但是这种使用web工厂的方式太麻烦了,在开发中并不会使用这种方法,所以我就要介绍spring整合struts2框架的另外一种方法。目前有两种方法,现在介绍第二种方式,第一种方式见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10143918.html。
2. 把具体的Action类配置文件applicatonContext.xml的配置文件中,但是注意:struts.xml需要做修改:
我们需要在applicationContext.xml中定义action,action由spring来进行管理,在原有配置applicationContext.xml中添加下面这组语句:
<!-- 将action由spring来管理 -->
<!-- 通过scope设置来设置action为多例 -->
<bean id="customerAction" class="com.huida.web.CustomerAction" scope="prototype">
<!-- 必须手动注入service属性 -->
<property name="customerService" ref="customerService"></property>
</bean>
完整的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="customerService" class="com.huida.service.CustomerServiceImpl"> </bean> <!--
需要主要的两点:
1.struts.xml对应的action标签中的class属性填充的是spring中applicationContext.xml中对应Bean的Id
2.一定要给我们的action配置多例
-->
<!-- 将action由spring来管理 -->
<!-- 通过scope设置来设置action为多例 -->
<bean id="customerAction" class="com.huida.web.CustomerAction" scope="prototype">
<!-- 必须手动注入service属性 -->
<property name="customerService" ref="customerService"></property>
</bean> </beans>
3. 更改完applicationContext.xml后,由于我们在这里面定义了Action,所以我们在struts.xml中定义action的时候,在action标签中的class属性不用写全路径了,而填充的是spring中applicationContext.xml中对应Bean的Id。更改后的配置内容为:
<?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="crm" namespace="/" extends="struts-default">
<!-- action的全路径写在applicationContext.xml中 -->
<action name="customer_*" class="customerAction" method="{1}">
<result name=""></result>
</action>
</package>
</struts>
4. 我们的Service同样使用上一篇博文的方式来进行获取,在Action中使用数据封装的方式进行获取,所以我们只需要在CustomerAction中定义CustomerService对象,然后定义其Set方法即可:
package com.huida.web; import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import com.huida.domain.Customer;
import com.huida.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{ //手动实例化对象
//private Customer customer=new Customer(); private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
} /*
* 保存客户的方法
*/
public String save(){
System.out.println("Action中执行了save方法");
customerService.save();
return NONE;
} @Override
public Customer getModel() { return null;
}
}
5. 我们可以验证一下struts与spring整合是否成功。
启动服务器-->在浏览器中输入http://localhost:8080/ssh1-->在页面中点击客户管理-->新增客户-->点击保存按钮。在控制台上输出如下内容:

通过以上步骤我们便将struts与spring通过传统的方法整合起来了。
6.第二种方式需要有几个注意的地方:
(1)struts.xml对应的action标签中的class属性填充的是spring中applicationContext.xml中对应Bean的Id
(2)pring框架默认生成CustomerAction是单例的,而Struts2框架是多例的。所以需要配置 scope="prototype"
(3)CustomerService现在必须自己手动注入了。
Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)的更多相关文章
- Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...
- 一 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有一 ...
- Spring框架学习(5)spring整合struts2
内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...
- Spring集成Quartz框架的两种方式。
可参考:https://blog.csdn.net/yk614294861/article/details/84324603 1.使用Spring与Quarta配置作业得两种方式: a.方式一,Met ...
- Spring中bean实例化的三种方式
之前我已经有好几篇博客介绍Spring框架了,不过当时我们都是使用注解来完成注入的,具体小伙伴可以参考这几篇博客(Spring&SpringMVC框架案例).那么今天我想来说说如何通过xml配 ...
- Spring整合Struts2的方法
一.基本支持 通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器.也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中. ...
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
- Spring管理连接池的几种方式
第一种方式:.Spring常规的数据库连接方法: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations=&qu ...
- 【websocket】spring boot 集成 websocket 的四种方式
集成 websocket 的四种方案 1. 原生注解 pom.xml <dependency> <groupId>org.springframework.boot</gr ...
随机推荐
- class装载原理
原理图: 1.执行引擎是Java虚拟机实现的核心,用于处理各种指令. 2.PC寄存器用于存储线程下一次指令的地址和返回值地址,虚拟机为每个线程创建单独的PC寄存器.如果执行的是本地方法,PC寄存器的值 ...
- 一点ExtJS开发的感悟
虽然项目一直采用ExtJS作为前端开发,接触到了一些ExtJS 的一些场景界面,自己也尝试封装一些组件,对于开发70%基本可以满足需求.遇到最为麻烦的就是Ext的模版或者直接拼接字符串再进行eval转 ...
- MySQL ALTER讲解
当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@host# mysql -u r ...
- 详解 Facebook 田渊栋 NIPS2017 论文:深度强化学习研究的 ELF 平台
这周,机器学习顶级会议 NIPS 2017 的论文评审结果已经通知到各位论文作者了,许多作者都马上发 Facebook/Twitter/Blog/ 朋友圈分享了论文被收录的喜讯.大家的熟人 Faceb ...
- CSS 3 学习笔记
css 3 学习笔记 文本: word-wrap : normal | break-word取值:normal: 控制连续文本换行.break-word: 内容将在边界内换行.如果需要,词 ...
- 关于tcp的三次握手与四次挥手,以及粘包
tcp三次握手: TCP是因特网中的传输层协议,使用三次握手协议建立连接.当主动方发出SYN连接请求后,等待对方回答SYN+ACK[1],并最终对对方的 SYN 执行 ACK 确认.这种建立连接的方法 ...
- memcache.so的报错信息,未解决
memcache.so php版本5.6 executor_globals_id in Unknown on line 0 编译也成功了,路径也是在其他so文件的目录 但是加载失败的,查看apache ...
- 《汇编语言 基于x86处理器》前五章的小程序
▶ 书中前五章的几个小程序,基本的运算操作,使用了作者的库 Irvine32 和 Irvine64(一开始以为作者网站过期了,各网站上找到的文件大小都不一样,最后发现是要搭梯子 Orz,顺利下载).注 ...
- leetcode965
public class Solution { List<int> list = new List<int>(); private void postTree(TreeNode ...
- mysql大表优化
https://segmentfault.com/a/1190000006158186 https://tech.meituan.com/mysql-index.html http://www.cnb ...