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. Log4net详细说明(全)

    转自:http://www.cnblogs.com/zhangchenliang/p/4546352.htmlhttp://www.cnblogs.com/zhangchenliang/p/45463 ...

  2. ffmpeg 编码(视屏)

    分析ffmpeg_3.3.2 muxing 1:分析主函数,代码如下: int main(int argc, char **argv) { OutputStream video_st = { }, a ...

  3. solr4.x之原子更新

    solr4.x发布以后,最值得人关注的一个功能,就是原子更新功能,传说的solr是否能真正的做到像数据库一样,支持单列更新呢? 在solr官方的介绍中,原子更新是filed级别的更新,不会涉及整个Do ...

  4. nginx经过多层代理后获取真实来源ip

    nginx取 $remote_addr 当做真实ip,而事实上,$http_X_Forwarded_For 才是用户真实ip,$remote_addr只是代理上一层的地址 解决方案: 在 http 模 ...

  5. c#面向对象基础4

    一.namespace 命名空间 作用:解决不同类重名的问题  我们可以认为类是属于命名空间的 当我们需要再一个类中与另一个类建立关系时,通过命名空间来区别不同的类.所以需要我们这样做:导入命名空间 ...

  6. Ubuntu jdk 8 与 6 切换 (安装与配置)

    Switch To Oracle JDK8 Switch To Oracle JDK8 1.1 Switch Oracle JDK in the Unbuntu 14.04 Step1 : Downl ...

  7. 1.java生成随机不重复10位字符串

    package org.changneng.util; import java.util.Random; public class A { public static void main(String ...

  8. JSON.Stringify()和JSON.parse()的比较使用

    1.  JSON.Stringify() 将一个对象解析成字符串 <script> function myonclick() { var value = $('select option: ...

  9. 16 python xml模块

    1.基本概念 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单. 不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀. 至今很多传统公司如金 ...

  10. ABAP-面向对象的开发

    转载:https://blog.csdn.net/zhongguomao/article/details/70266246 在程序中, 对象的识别和寻址是通过对象引用来实现的,对象引用变量可以访问对象 ...