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;
} @Override
public 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.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>
</beans>

6. 我们可以验证一下struts与spring整合是否成功。

启动服务器-->在浏览器中输入http://localhost:8080/ssh1-->在页面中点击客户管理-->新增客户-->点击保存按钮。在控制台上输出如下内容:

通过以上步骤我们便将struts与spring通过传统的方法整合起来了。但是这种整合不是我推荐的方法,所以在下一篇博文中我就对将我推荐的方法进行总结。

Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。的更多相关文章

  1. 使用CSS中的meta实现web定时刷新或跳转的方法

    这篇文章主要介绍了使用CSS中的meta实现web定时刷新或跳转的方法,比使用JavaScript脚本实现起来更加简单一些,需要的朋友可以参考下 meta源信息功能之页面定时跳转与刷新 几乎所有的网页 ...

  2. 集合ArrayList双色球练一练(自己的方法,太麻烦)

    namespace 集合__双色球练一练{    class Program    {        static void Main(string[] args)        {        / ...

  3. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  4. Maven环境下搭建SSH框架之Spring整合Hibernate

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...

  5. 简易的CRM系统案例之Struts2&Spring整合+Hibernate3+JSP+MySQL版本

    主要对上一篇Struts2&Spring整合的改造 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本 src/bean.xml <beans xmlns ...

  6. C#不用union,而是有更好的方式实现 .net自定义错误页面实现 .net自定义错误页面实现升级篇 .net捕捉全局未处理异常的3种方式 一款很不错的FLASH时种插件 关于c#中委托使用小结 WEB网站常见受攻击方式及解决办法 判断URL是否存在 提升高并发量服务器性能解决思路

    C#不用union,而是有更好的方式实现   用过C/C++的人都知道有个union,特别好用,似乎char数组到short,int,float等的转换无所不能,也确实是能,并且用起来十分方便.那C# ...

  7. springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题

    解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...

  8. Spring整合freemarker发送邮件

    转载:http://blog.csdn.net/zdp072/article/details/32745335 分类: freemarker spring 2014-06-20 23:39 752人阅 ...

  9. hisql ORM 框架研究(国内第一个支持HANA的ORM框架)

    HiSql 操作说明文档 V1.0 下一代ORM框架 国内第一个支持HANA的ORM框架 hisql源码下载 git clone https://github.com/tansar/HiSql.git ...

随机推荐

  1. 基于标准库实现string和wstring的转换

    // convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...

  2. DIY-组装

    DIY:-组装 组装,现在基本什么都可以组装,就像计算机,手机,自己进行定制,同样操作系统可以自己组装,软件开发也要组装,现在就是一个DIY的时代. 大家了解DIY,说白了就是自己定制组装一些东西,比 ...

  3. 学习MongoDB 二:MongoDB添加、删除、修改

    一.简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSQL数据库产品中最热门的一种.数据被分组存储在数据集中,被称为一个集合(Collenction)和对于存储在MongoDB ...

  4. python基础补充内容

    知识内容: 1.三元运算表达式 2.python代码编写规范 3.模块导入与使用 4.python文件名 5.python脚本的"__name__"属性 6.python之禅 一. ...

  5. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165233

    Exp0 Kali安装 安装过程 1.首先我的Mac上已经安装好了VMware Fusion,所以直接下载对应的虚拟机版本的Kali即可. 2.进入Kali官网进行下载. 以下为下载链接: Kali ...

  6. 图片水平垂直居中(兼容IE6,IE7,firefox,opera,safari,其中图片可以是任何块元素)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. HTML 标签元素的 align 属性

    align 属性规定段落中文本的对齐方式. 有 left  right center  justify 这些参数 left  right center  就是左对齐 右对齐 中间对齐 justify  ...

  8. Centos LVM 创建 删除 扩大 缩小

    新建LVM的过程1.使用fdisk 新建分区 修改ID为8e3.使用 pvcreate 创建 PV 4.使用 vgcreate 创建 VG 5.使用 lvcreate 创建 LV 6.格式化LV7.挂 ...

  9. MySQL数据类型的长度

    MySQL有几种数据类型可以限制类型的"长度",有CHAR(Length).VARCHAR(Length).TINYINT(Length). SMALLINT(Length).ME ...

  10. WP8.1 页面跳转,overwrite后退键

    In 8.1 we use the below code to navigate between pages: this.Frame.Navigate(typeof(PivotPage)); In 8 ...