Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。...
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/10144175.html。
2. 因为导入的struts2-spring-plugin-2.3.24.jar 包自带一个配置文件 struts-plugin.xml ,该配置文件中有如下代码:<constant name="struts.objectFactory" value="spring" /> 开启一个常量,如果该常量开启,那么下面的常量就可以使用struts.objectFactory.spring.autoWire = name,该常量是可以让Action的类来自动装配Bean对象!!
3.更改CustomerAction代码,不是通过原来的web工厂的方式,而是通过数据封装的方式(使用了里面的模型封装),代码如下:
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;}@Overridepublic Customer getModel() {return null;} }
4.action由struts2创建,所以需要在strutsx.xml中配置action,配置内容为:
<?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 name="customer_*" class="com.huida.web.CustomerAction" method="{1}"><result name=""></result></action> </package> </struts>
5.在applicationContext.xml中配置service,这样在加载配置文件的时候会扫描到CustomerService,然后为其创建对象,配置内容为:
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><bean id="customerService" class="com.huida.service.CustomerServiceImpl"></bean> </beans>
6. 我们可以验证一下struts与spring整合是否成功。
启动服务器-->在浏览器中输入http://localhost:8080/ssh1-->在页面中点击客户管理-->新增客户-->点击保存按钮。在控制台上输出如下内容:
通过以上步骤我们便将struts与spring通过传统的方法整合起来了。但是这种整合不是我推荐的方法,所以在下一篇博文中我就对将我推荐的方法进行总结。
转载于:https://www.cnblogs.com/wyhluckdog/p/10143918.html
Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。...相关推荐
- abp.ajax get,ABP框架中导航菜单的使用及JavaScript API获取菜单的方法
每一个WEB应用程序都有导航菜单,Abp也为用户提供了通用的创建和显示菜单方式. 创建菜单一个应用程序可能包含不同的模块,而每个模块都可能有它自己的菜单项.在Abp中,需要创建一个派生自Navigat ...
- 浅谈“==”、equals和hashcode,以及map的遍历方法(可用作上一篇k-means博文参考)
前不久看到一个公司的面试题,问到"=="和"equals"的区别,些许上答不上来,于是木羊搜索并整理了一下.此外,木羊前面写了k-means算法实现的博文,其中 ...
- CXF开发及与Spring整合开发
前言 本篇承接上一篇 Java CXF介绍与实例 开发环境: Java: 1.8.0_161 Eclipse Version: Oxygen.3 Release (4.7.3) Maven: 3.5. ...
- java获取项目路径方法_java项目获取路径的几种方式
第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...
- 【新手宝典】一篇博文带萌新建站并了解建站体系流程和对萌新友好的便捷方式,这篇博文很有可能是你的启蒙文
前言 本片博文主要面向于还没接触过web开发的萌新,以及想知道整体流程并且完成建站的萌新:如果你是个大佬,就没必要看下去了. 本篇博文没有难啃的骨头,请各位萌新放心食用. 本篇博文采用通俗易懂的方式讲 ...
- vue 多个click_vue中touch和click共存的解决方式
在vue开发手机端项目时,我们经常会遇到touch和click共存的情况,比如城市选择页面的字母滑动 touch事件在手机端会触发浏览器滑动,所以应给touch事件添加阻止默认行为touchmove. ...
- 帝国cms内容页调用php,帝国CMS内容页调用上一篇与下一篇方法汇总
本文实例汇总了帝国CMS内容页调用上一篇与下一篇方法.分享给大家供大家参考.具体方法如下: 一.动态调用上一篇下一篇链接 上一篇动态调用上一篇链接 下一篇动态调用下一篇链接 二.灵动标签和SQl语句调 ...
- 常见的五种神经网络(3)-循环神经网络(上)篇
转载请注明出处:https://thinkgamer.blog.csdn.net/article/details/100600661 博主微博:http://weibo.com/234654758 G ...
- android列表滑到底部,Android中判断listview是否滑动到顶部和底部的实现方法
今天实现listview的下拉刷新和上拉加载的时候,遇到了一个问题,*就是说需要根据listview中滑动的位置来进行下拉刷新和上拉加载.* 具体点,只有当我的listview滑动到最顶部的时候,这时 ...
最新文章
- python将二维列表内容写入和读取.txt文件
- idc 计算机行业数据分析,IDC:2015年计算机科技行业趋势预测
- Java中proc是什么意思,在java里头读取/proc/net/dev
- 给 Flutter 界面切换来点特效
- 深度学习中的内存管理问题研究综述
- python调试神器_你必须拥有的Python调试神器
- ubuntu desktop的分辨率设置
- 天龙hJ线程发包处理方法如何跳出线程发包
- 基于稀疏表示理论的图像去噪
- 基于51单片机的数字万用表设计
- 图论与代数结构 最短路问题
- IIS 部署的网站无法启动
- 数据挖掘肿瘤预测_肿瘤分析数据挖掘及信息解读
- import time python_Python——入门级(import 模块)
- HCIA Storage部分题库
- 【操作系统】操作系统的功能管理与分类
- HP打印机M329dw纸盘关闭卡、涩问题解决
- Veins modules
- 项目管理第八章项目质量管理
- 官方也无力回天?“SharedPreferences 存在什么问题?”