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. javascript节点操作appendChild()

    cloneNode(a)方法接受一个布尔值参数,表示是否深拷贝 true:表示执行深拷贝,复制本节点以及整个子节点树. false:浅拷贝.只复制节点本身. 复制后返回的节点副本属于文档所有,但是并没 ...

  2. 关于oracle数据库压力测试

    今天接到需求,需要对oracle数据库进行压力测试,就这几个字的需求. 然后查看了以下软件: 1.Benchmark Factory是一款专业的服务器性能测试工具,专为数据库测试和可扩展性测量而设计, ...

  3. navicate for mysql mac 含注册机 亲测可用

    百度网盘:https://pan.baidu.com/s/1hrXnRes

  4. 异步请求fetch之初体验

    更好阅读体验可移步我的博客:Blog 导读 传递信息到服务器,从服务器获取信息,是前端发展的重中之重,尤其是现在前后端分离的大前提下,前后端的数据交互是前端的必修科目了.从很久之前到现在,ajax都是 ...

  5. shutil模块---文件,文件夹复制、删除、压缩等处理

    shutil模块:高级的文件,文件夹,压缩包处理 拷贝内容 # shutil.copyfileobj(open('example.ini','r'),open('example.new','w')) ...

  6. cplex-Java-样例代码解析

    import ilog.cplex.IloCplex; import ilog.concert.*; /** * * * * 最大化 x1 + 2x2 + 3x3</br> * 约束 &l ...

  7. OpenACC 绘制曼德勃罗集

    ▶ 书上第四章,用一系列步骤优化曼德勃罗集的计算过程. ● 代码 // constants.h ; ; ; ; const double xmin=-1.7; ; const double ymin= ...

  8. Redis 的 GEO 特性将在 Redis 3.2 版本释出

    Redis 的 GEO 特性将在 Redis 3.2 版本释出, 这个功能可以将用户给定的地理位置信息储存起来, 并对这些信息进行操作. 本文将对 Redis 的 GEO 特性进行介绍, 说明这个特性 ...

  9. CUDA入门

    CUDA入门 鉴于自己的毕设需要使用GPU CUDA这项技术,想找一本入门的教材,选择了Jason Sanders等所著的书<CUDA By Example an Introduction to ...

  10. shell echo 打印换行

    echo -e "aaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbb"