Spring和Struct整合的三个方法
1.使用Spring 的 ActionSupport 。
2.使用Spring 的 DelegatingRequestProcessor 类。
3.全权委托。
无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。 就是在 struts 中加入一个插件。struts-config.xml中
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property property=”contextConfigLocation” value=”/WEB-INF/applicationContext.xml”/>
</plug-in>
spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml 在WEB-INF目录下面
1,使用Spring的ActionSupport .
Spring 的ActionSupport 继承至 org.apache.struts.action.Action
ActionSupport的子类可以或得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。
这是一个 servlet 的代码:
- public class LoginAction extends org.springframework.web.struts.ActionSupport {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
- //获得 WebApplicationContext 对象
- WebApplicationContext ctx = this.getWebApplicationContext();
- LoginDao dao = (LoginDao) ctx.getBean("loginDao");
- User u = new User();
- u.setName(loginForm.getName());
- u.setPwd(loginForm.getPwd());
- if(dao.checkLogin(u)){
- return mapping.findForward("success");
- }else{
- return mapping.findForward("error");
- }
- }
- }
applicationContext.xml 中的配置
<beans>
<bean id=”loginDao” class=”com.cao.dao.LoginDao”/>
</beans>
这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自 org.springframework.web.struts.ActionSupport 使得struts和spring耦合在一起。
但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean(“loginDao”))。
2,使用Spring 的 DelegatingRequestProcessor 类
DelegatingRequestProcessor 继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。sturts-config.xml 中 <controller processorClass=”org.springframework.web.struts.DelegatingRequestProcessor”/> 通过 <controller >来替代 org.apache.struts.action.RequestProcessor 的请求处理。
- public class LoginAction extends Action {
- //利用spring来注入这个对象。
- private LoginDao dao ;
- public void setDao(LoginDao dao) {
- System.out.println("执行注入");
- this.dao = dao;
- }
- public LoginDao getDao() {
- return dao;
- }
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
- //这样一改这行代码似乎没有必要了。
- //WebApplicationContext ctx = this.getWebApplicationContext();
- //LoginDao dao = (LoginDao) ctx.getBean("loginDao");
- User u = new User();
- u.setName(loginForm.getName());
- u.setPwd(loginForm.getPwd());
- //直接用dao来调用spring会将这个对象实例化。
- if(dao.checkLogin(u)){
- return mapping.findForward("success");
- }else{
- return mapping.findForward("error");
- }
- }
- }
这里的LoginAction extends Action 说明 struts 每有和spring 耦合。
看一下applicationContext.xml 中的配置。
<beans>
<bean id=”loginDao” class=”com.cao.dao.LoginDao”/>
<bean name=”/login” class=”com.cao.struts.action.LoginAction”>
<property name=”dao”>
<ref local=”loginDao”/>
</property>
</bean>
</beans>
这里name=”/login”与struts中的path匹配class=”com.cao.struts.action.LoginAction”与struts中的type匹配还要为LoginAction提供必要的setXXX方法。获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。
3,全权委托:
Action 的创建和对象的依赖注入全部由IOC容器来完成。使用Spring的DelegatingActionProxy来帮助实现代理的工作
org.springframework.web.struts.DelegatingActioProxy继承于org.apache.struts.action.Action.
全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。
1, <action>中 type指向的是spring 的代理类
<struts-config>
<data-sources />
<form-beans >
<form-bean name=”loginForm” type=”com.cao.struts.form.LoginForm” />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<!– type指向的是spring 的代理类 –>
<action
attribute=”loginForm”
input=”login.jsp”
name=”loginForm”
path=”/login”
scope=”request”
type=”org.springframework.web.struts.DelegatingActionProxy” >
<forward name=”success” path=”/ok.jsp” />
<forward name=”error” path=”/error.jsp” />
</action>
</action-mappings>
<message-resources parameter=”com.cao.struts.ApplicationResources” />
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property property=”contextConfigLocation” value=”/WEB-INF/applicationContext.xml”/>
</plug-in>
</struts-config>
2, 去掉struts-config.xml中 <controller >
三种整和方式中我们优先选用 全权委托的方式。
理由:
1,第一种使得过多的耦合了Spring和Action .
2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。
总结一下:
整合工作中的步骤:
1.修改struts-config.xml
2. 配置applicationContext.xml
3.为Action添加get/set方法 来获得依赖注入的功能。
Spring和Struct整合的三个方法的更多相关文章
- Spring框架中整合JUnit单元测试的方法
一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...
- Spring Boot 注册 Servlet 的三种方法,真是太有用了!
本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spr ...
- 009-shiro与spring web项目整合【三】验证码、记住我
一.验证码 1.自定义FormAuthenticationFilter 需要在验证账号和名称之前校验验证码 /** * * <p>Title: CustomFormAuthenticati ...
- Spring、实例化Bean的三种方法
1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...
- spring集成JPA的三种方法配置
JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...
- MyBatis 与 Spring 的完美整合方法
MyBaits 整合 Spring MyBatis-Spring 项目 第一步:创建测试工程 第二步:引入依赖 jar 包 第三步:编写 Spring 配置文件 第四步:编写 MyBatis 配置文件 ...
- Spring使用jdbcJdbcTemplate和三种方法配置数据源
三种方法配置数据源 1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar <!-- spring内置,springJdbc,配置数据源 --> <bean ...
- spring注入bean的三种方法
在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
- Spring实例化Bean三种方法:构造器、静态工厂、实例工厂
Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...
随机推荐
- oracle命令的缩写原型单词方便记忆总结
$ORACLE_HOME/bin下的utilities解释 Binary First Available Description adapters ...
- AppStore上架规则
1. 条款和条件1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同.以下规 ...
- An App ID with Identifier 'xxxxxx’ is not available. Please ....
1.完全关闭Xcode; 2.找到钥匙串,将钥匙串(Keychain)中的对应证书移除: 3.再次打开Xcode,通过 Preferences - Account 4. 删除原先的账号重新登录, 搞定 ...
- 利用CMake自己创建OpenCV静态链接库
1.准备工作: 1)完成Visual Studio2012安装: 2)下载并解压CMake3.5.0: 3)下载并解压OpenCV2.4.12: 4)下载并解压TBB44_20160128oss. 2 ...
- java.math.BigDecimal类
BigDecimal类用于高精度计算.一般的float型和Double型数据只可以用来做科学计算或者是工程计算,由于在商业计算中,要求的数字精度比较高,所以要用到java.math.BigDecima ...
- VLC命令参数(转载)
转载自: http://blog.csdn.net/bytxl/article/details/6613449 http://www.cnblogs.com/MikeZhang/archive/201 ...
- Linux下定时备份数据库
linux下使用crontab定时备份MYSQL数据库的方法只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: mkdir /var/lib/mysqlbackup cd ...
- 第一个程序点亮一个LED灯
#include <reg52.h> // 引用52包文件 可以理解为命名空间 sbit P1_0 = P1^0; // 定义P1管脚0 void main() ...
- SignalTap II应用小实例之触发位置
概述 SignalTap II一直以来都是笔者调试Altera FPGA设计的利器,最近比较有时间静下心来研究SignalTap II某些细节,虽然笔者有过不少关于SignalTap的使用,且也发表过 ...
- 【深入浅出jQuery】源码浅析2--使用技巧
最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...