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 的代码:

  1. public class LoginAction extends org.springframework.web.struts.ActionSupport {
  2. public ActionForward execute(ActionMapping mapping, ActionForm form,
  3. HttpServletRequest request, HttpServletResponse response) {
  4. LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  5. //获得 WebApplicationContext 对象
  6. WebApplicationContext ctx = this.getWebApplicationContext();
  7. LoginDao dao = (LoginDao) ctx.getBean("loginDao");
  8. User u = new User();
  9. u.setName(loginForm.getName());
  10. u.setPwd(loginForm.getPwd());
  11. if(dao.checkLogin(u)){
  12. return mapping.findForward("success");
  13. }else{
  14. return mapping.findForward("error");
  15. }
  16. }
  17. }

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 的请求处理。

  1. public class LoginAction extends Action {
  2. //利用spring来注入这个对象。
  3. private LoginDao dao ;
  4. public void setDao(LoginDao dao) {
  5. System.out.println("执行注入");
  6. this.dao = dao;
  7. }
  8. public LoginDao getDao() {
  9. return dao;
  10. }
  11. public ActionForward execute(ActionMapping mapping, ActionForm form,
  12. HttpServletRequest request, HttpServletResponse response) {
  13. LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  14. //这样一改这行代码似乎没有必要了。
  15. //WebApplicationContext ctx = this.getWebApplicationContext();
  16. //LoginDao dao = (LoginDao) ctx.getBean("loginDao");
  17. User u = new User();
  18. u.setName(loginForm.getName());
  19. u.setPwd(loginForm.getPwd());
  20. //直接用dao来调用spring会将这个对象实例化。
  21. if(dao.checkLogin(u)){
  22. return mapping.findForward("success");
  23. }else{
  24. return mapping.findForward("error");
  25. }
  26. }
  27. }

这里的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整合的三个方法的更多相关文章

  1. Spring框架中整合JUnit单元测试的方法

    一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...

  2. Spring Boot 注册 Servlet 的三种方法,真是太有用了!

    本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spr ...

  3. 009-shiro与spring web项目整合【三】验证码、记住我

    一.验证码 1.自定义FormAuthenticationFilter 需要在验证账号和名称之前校验验证码 /** * * <p>Title: CustomFormAuthenticati ...

  4. Spring、实例化Bean的三种方法

    1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...

  5. spring集成JPA的三种方法配置

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...

  6. MyBatis 与 Spring 的完美整合方法

    MyBaits 整合 Spring MyBatis-Spring 项目 第一步:创建测试工程 第二步:引入依赖 jar 包 第三步:编写 Spring 配置文件 第四步:编写 MyBatis 配置文件 ...

  7. Spring使用jdbcJdbcTemplate和三种方法配置数据源

    三种方法配置数据源 1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar <!-- spring内置,springJdbc,配置数据源 --> <bean ...

  8. spring注入bean的三种方法

    在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

  9. Spring实例化Bean三种方法:构造器、静态工厂、实例工厂

    Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...

随机推荐

  1. ExtJs 第二章,Ext.form.Basic表单操作

    1.认识Ext.form.Panel表单面板         Ext.form.field.CheckBox 复选框 checkboxfield Ext.form.CheckBoxGroup 复选框组 ...

  2. AppiumDriver 运行app启动基本参数

    记录一下 DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(Mobile ...

  3. apache 服务器端口被IIS服务器占用

    今天遇到一个问题 同事机子上安装了wamp运行环境,所有服务也打开了,把dede系统放到了www目录下面,但是打开localhost网址,一直是跳转到一个IIS的web服务器主页 想不到是什么问题,又 ...

  4. ubuntu 安装 open in teminal

    sudo apt-get install nautilus-open-terminalnautilus -q

  5. 那些年被我坑过的Python——道阻且长(第五章实用模块讲解)

    random模块 我的随机验证吗程序: 首先保证了字母和数字出现的概率是50% VS 50%,其次是可以订制输出多少位 def Captcha(size): Captcha_list = [] for ...

  6. IOS各类问题

    1.The resource could not be loaded because the App Transport Security policy requires the use of a s ...

  7. windows下实现uboot的tftp下载功能

    一.原理分析 带有uboot的开发板实际上充当的就是tftp客户端,而PC机扮演的角色就是tftp服务器端,而tftp下载功能实际上就是文件传输.tftp服务器可以建立在虚拟机linux下,也可以建立 ...

  8. SharePoint2013 Powershell script to get site Title, Site Owner, Site user count and usage

    Powershell script to get site Title, Site Owner, Site user count and usage Add-PSSnapin microsoft.sh ...

  9. ios入门之c语言篇——基本函数——4——数值交换函数

    一个常用函数,被整理出来,免得每次 都要写 参数返回值解析: 参数: *a:int*,需要交换值的第一个变量: *b:int*,需要交换值的第二个变量: 返回值: (无) 函数解析: swap(&am ...

  10. RichEdit中插入带背景色文本的一种思路

    uses RichEdit; function TextToRtf( // 将文本处理为RTF格式 mText: WideString // 输入文本 ): WideString; // 返回处理后的 ...