Spring框架整合Struts2框架的传统方法
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框架的传统方法的更多相关文章
- Spring 框架整合Struts2 框架和 Hibernate 框架
1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...
- 整合Struts2框架和Spring框架
-----------------------siwuxie095 整合 Struts2 框架和 Spring 框架 1 ...
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...
- Maven项目整合Struts2框架
-------------------------siwuxie095 Maven 项目整合 Struts2 框架 1. ...
- Spring_day04--Spring框架整合hibernate框架
Spring框架整合hibernate框架 1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置 2 把hibernate里面的sessionFactory创建交给 ...
- shiro框架整合ssm框架
下面我通过一个web的maven项目来讲解如何将shiro整合ssm框架,具体结构如下图 一.引入依赖的jar包 <?xml version="1.0" encoding=& ...
- Spring笔记⑥--整合struts2
Spring如何在web应用里面用 需要额外加入的jar包 Spring-web-4.0.0 Spring-webmvc-4.0.0 Spring的配置文件,没什么不同 需要在web.xml下配置 ...
- Spring框架整合Struts2
1,用Spring架构,及Struts2-spring-plugin插件 导入Spring的dist全部所需的jar包 Struts2的spring插件 struts2-spring-plugin.X ...
随机推荐
- javascript创建对象之构造函数模式(二)
对上一章节的工厂模式进行代码重写 function Human(name, sex) { this.name = name; this.sex = sex; this.say = function ( ...
- Dependency Injection in ASP.NET Web API 2 Using Unity
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
- 浅析PHP7新功能及语法变化总结
标量类型声明 有两种模式: 强制 (默认) 和 严格模式. 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 ...
- noip 2011 选择客栈
题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...
- Fork-Join分治编程介绍(一)
一.Fork-Join 框架介绍 1. 什么是 Fork-Join 分治编程框架 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每 ...
- python学习之----异常处理小示例
网络是十分复杂的.网页数据格式不友好,网站服务器宕机,目标数据的标签找不到,都 是很麻烦的事情.网络数据采集最痛苦的遭遇之一,就是爬虫运行的时候你洗洗睡了,梦 想着明天一早数据就都会采集好放在数据库里 ...
- HTML5进阶
内容: 1.geolocation元素 2.video元素和audio元素 3.localStorage 4.WebWorker 5.WebSQL.IndexedDB 6.文件操作.文件拖拽新概念 7 ...
- php使用tcpdf实现在线PDF功能
今天看书,发现有个例子就是实现php生成pdf格式文件的例子,所以扩展了下百度了下 找了个tcpdf Git上有地址,如果感冒自行下载 https://github.com/tecnickcom/tc ...
- [Illuminate\Database\QueryException] SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using pas sword: NO) (SQL: select * from information_schema.tables where table_schema = la
[Illuminate\Database\QueryException] SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost ...
- 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结
第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...