1. 导入CRM项目的UI页面,找到添加客户的页面,修改form表单,访问Action

  * 将menu.jsp中133行的新增客户的跳转地址改为:href="${pageContext.request.contextPath}/jsp/customer/add.jsp"

  * 将jsp/customer下的add.jsp的提交页面的地址改为:action="${pageContext.request.contextPath }/customer_save"。当点击保存按钮之后,访问customer这个action中的save方法。

2. 创建包结构:

  * ssh1

    * com.huida.dao

    * com.huida.domain

    * com.huida.service

    *com.huida.web

3. 在com.huida.web下创建action类CustomerAction。

 在com.huida.service下创建接口:CustomerService,实现接口的类:CustomerServiceImpl。

 CustomerService接口中的方法为:

package com.huida.service;

public interface CustomerService {

    public void save();
}

CustomerServiceImpl实现类的内容为:

package com.huida.service;

public class CustomerServiceImpl implements CustomerService {

    @Override
public void save() { System.out.println("Service层中的save方法被执行了"); } }

4. 在applicationContext.xml中配置我们的service。配置内容为:

<?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>

5.编写CustomerAction接收请求,在struts.xml中完成Action的配置

CustomerAction的接收请求为:

package com.huida.web;import com.huida.domain.Customer;
import com.huida.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;public class CustomerAction extends ActionSupport{ /*
* 保存客户的方法
*/
public String save(){
System.out.println("Action中执行了save方法");
return NONE;
} }

在struts.xml中对Action的配置为:

<package name="crm" namespace="/" extends="struts-default">
<action name="customer_*" class="com.huida.web.CustomerAction" method="{1}">
<result name=""></result>
</action>
</package>

6.在Action中获取到service(开发不会使用,因为麻烦)

 * 可以通过 WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext()); 来获取,但是这种方式编写代码太麻烦了!!

 代码如下:

ackage 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; public class CustomerAction extends ActionSupport { /*
* 保存客户的方法
*/
public String save(){
System.out.println("Action中执行了save方法");
//传统的web工厂方法
WebApplicationContext webApplicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
CustomerService customerService=(CustomerService) webApplicationContext.getBean("customerService");
customerService.save();
return NONE;
}
}

* 还可以通过new ClassPathXmlApplicationContext("applicationContext.xml")来获取。

 代码如下:

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 { /*
* 保存客户的方法
*/
public String save(){
System.out.println("Action中执行了save方法");
//使用spring的工厂
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService customerService=(CustomerService) ac.getBean("customerService"); customerService.save();
return NONE;
} }

通过这两种方法我们都可以实现从action中获取service。

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

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

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

Spring框架整合Struts2框架的传统方法的更多相关文章

  1. Spring 框架整合Struts2 框架和 Hibernate 框架

    1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...

  2. 整合Struts2框架和Spring框架

    -----------------------siwuxie095                                 整合 Struts2 框架和 Spring 框架         1 ...

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

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

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

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

  5. Maven项目整合Struts2框架

    -------------------------siwuxie095                                 Maven 项目整合 Struts2 框架         1. ...

  6. Spring_day04--Spring框架整合hibernate框架

    Spring框架整合hibernate框架 1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置 2 把hibernate里面的sessionFactory创建交给 ...

  7. shiro框架整合ssm框架

    下面我通过一个web的maven项目来讲解如何将shiro整合ssm框架,具体结构如下图 一.引入依赖的jar包 <?xml version="1.0" encoding=& ...

  8. Spring笔记⑥--整合struts2

    Spring如何在web应用里面用 需要额外加入的jar包 Spring-web-4.0.0 Spring-webmvc-4.0.0 Spring的配置文件,没什么不同   需要在web.xml下配置 ...

  9. Spring框架整合Struts2

    1,用Spring架构,及Struts2-spring-plugin插件 导入Spring的dist全部所需的jar包 Struts2的spring插件 struts2-spring-plugin.X ...

随机推荐

  1. Spark学习笔记2:RDD编程

    通过一个简单的单词计数的例子来开始介绍RDD编程. import org.apache.spark.{SparkConf, SparkContext} object word { def main(a ...

  2. unity3d动态加载dll的API以及限制

    Unity3D的坑系列:动态加载dll 一.使用限制 现在参与的项目是做MMO手游,目标平台是Android和iOS,iOS平台不能动态加载dll(什么原因找乔布斯去),可以直接忽略,而在Androi ...

  3. 初步认识AutoMapper

      AutoMapper 初步认识AutoMapper 前言 手动映射 使用AutoMapper 创建映射 Conventions 映射到一个已存在的实例对象   前言 通常在一个应用程序中,我们开发 ...

  4. django的forms

    base知识 参考博文:https://www.cnblogs.com/yuanchenqi/articles/9036474.html 用户表单是Web端的一项基本功能,大而全的Django框架中自 ...

  5. 你的GAN训练得如何--GAN 的召回率(多样性)和精确率(图像质量)方法评估

    生成对抗网络(GAN)是当今最流行的图像生成方法之一,但评估和比较 GAN 产生的图像却极具挑战性.之前许多针对 GAN 合成图像的研究都只用了主观视觉评估,一些定量标准直到最近才开始出现.本文认为现 ...

  6. CENTOS 挂载ntfs移动硬盘

    参考网址: http://www.it610.com/article/3368930.htm (较全)http://blog.51cto.com/ultrasql/1927672

  7. docker容器中搭建kafka集群环境

    Kafka集群管理.状态保存是通过zookeeper实现,所以先要搭建zookeeper集群 zookeeper集群搭建 一.软件环境: zookeeper集群需要超过半数的的node存活才能对外服务 ...

  8. c++Builder XE6 MD5 加密算法 BASE64 URL 编码

    xe6,xe7 BASE64XE6 MD5 加密算法Delphifunction MD5(const texto: string): string; var idmd5: TIdHashMessage ...

  9. 在spring引入log4j(非web项目)

    https://blog.csdn.net/u012578322/article/details/78012183 在spring中使用log4j 引入log4j软件包 配置log4j属性 加载log ...

  10. AS3 os与version 区别 使用Capabilities类获取Flash Player的信息

    AS3中flash.system.Capabilities类提供诸多静态的只读属性来描述应用程序当前所运行在的系统和运行时信息,如Flash Player,Adobe AIR,Flash Lite.通 ...