这篇博客摘自[http://blog.csdn.net/chendc201/article/details/8464008],

其中也有一些是自己增加的部分 .

第一步, 需要为 Struts 装载 Spring 应用上下文环境。有以下三种方式:

1) 在 struts-config.xml 中使用 Struts Plugin

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml,/WEB-INF/action-servlet.xml"/>
</plug-in>

2) 在 web.xml 中使用 ContextLoaderListener

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

3) 在 web.xml 中使用 ContextLoaderServlet

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

需要注意的是:

使用 Struts PlugIn 的方式加载 Spring 配置文件有可能导致 DWR 无法取得 Spring 中定义的 bean,因为 DWR 有可能先于 Struts 被访问使用,而此时 Struts 配置文件还未加载!因此,在 Spring、Struts 和 DWR 集成时,应该在 web.xml 中通过 ContextLoaderLisenter 或 ContextLoaderServlet 加载 Spring 配置文件。

最佳实践是使用 Struts PlugIn 的方式加载 Struts Action 配置文件 /WEB-INF/action-servlet.xml,而使用 ContextLoaderLisenter 或 ContextLoaderServlet 方式加载 Spring 配置文件 applicationContext.xml,通过两次加载完成 Spring所有配置文件的加载。

第二步, 整合Struts1 和 Spring, 也就是管理 action 的 method

使用 DelegatingActionProxy 将 Struts Action 管理权委托给 Spring

<form-beans>
<form-bean name="loginForm" type="com.xxx.yyy.struts.LoginForm" />
<!-- 省略很多-->
</form-beans>
<action-mappings>
<action
attribute="loginForm"
name="loginForm"
parameter="method"
path="/login"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="login" path="/login.jsp"/>
<forward name="main" path="/main.jsp"/>
<!-- 省略很多-->
    </action>
</action-mappings>

上面的 loginForm 我理解为一个 POJO , 和数据库中映射出来的实体类有一定的区别, formbean具有的属性会比较少, 用于封装表单数据, 便于传递 .

对应的在 Spring 配置文件中应该就有 action 的实现类, 注意 name 属性, 是带有斜杠的, 对应 action 配置的 path 属性.

<bean name="/login" class="com.xxx.yyy.struts.LoginAction">
<property name="xxxService">
<ref bean="xxxService"/>
</property>
<property name="yyyService">
<ref bean="yyyService"/>
</property>
<property name="zzzService">
<ref bean="zzzService"/>
</property>
</bean>

action 中 method 的编写和 Struts2 差不多, 只不过不是返回一个逻辑视图的 String , 而是一个包含了 String 的 ActionForward对象, 道理还是一样的 , 也就是逻辑视图 , 然后再去对应的 action 配置中寻找物理视图.

public class LoginAction extends BaseAction {
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
//处理逻辑 return mapping.findForward("main");
}
}

上面的 BaseAction继承自 Struts 核心控制器 DispatchAction, 并且注入必须的业务层的 bean.

转自[http://blog.csdn.net/chendc201/article/details/8464008]

spring 整合 Struts1.X [转]的更多相关文章

  1. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  2. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  3. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  4. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  5. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  6. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  7. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  8. Spring整合HBase

    Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...

  9. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

随机推荐

  1. P3871 [TJOI2010]中位数

    傻逼题 维护两个系统堆即可 #include<bits/stdc++.h> #define il inline #define vd void typedef long long ll; ...

  2. Linux中的mysql指令

    如何启动/停止/重启MySQL一.启动方式1.使用 service 启动:service mysqld start2.使用 mysqld 脚本启动:/etc/inint.d/mysqld start3 ...

  3. c语言数字图像处理(九):边缘检测

    背景知识 边缘像素是图像中灰度突变的像素,而边缘是连接边缘像素的集合.边缘检测是设计用来检测边缘像素的局部图像处理方法. 孤立点检测 使用<https://www.cnblogs.com/Gol ...

  4. 基础:enctype 包含上传input时必须(解决图片上传不成功问题)

    今天在做一个上传图片的时候,死活就是看不到传过去的值..对比了写法没发现问题,后来抱着试试看的心,查看下了 from里的写法.发现缺少了enctype.不了解这个用法,特意百度了下. enctype ...

  5. Windows ,获取硬盘物理序列号(VC++)

    #include <windows.h> BOOL GetHDID(PCHAR pIDBufer) {     HANDLE hDevice=NULL;    hDevice=::Crea ...

  6. ceilometer 源码分析(polling)(O版)

    一.简单介绍ceilometer 这里长话短说, ceilometer是用来采集openstack下面各种资源的在某一时刻的资源值,比如云硬盘的大小等.下面是官网现在的架构图 这里除了ceilomet ...

  7. java按照字节切割字符串,解决汉字的问题

    编写一个截取字符串的函数,输入为一个字符串,截取开始地址,截取字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个, 如“我ABC”,0,4,应该截为“我AB”,输入“我ABC汉DEF”,1, ...

  8. Geatpy遗传算法在曲线寻优上的初步探究

    园子里关于遗传算法的教案不少,但基于geatpy框架的并未多见,故分享此文以作参考,还望广大园友多多指教! Geatpy出自三所名校联合团队之手,是遗传算法领域的权威框架(python),其效率之高. ...

  9. hadoop 集群HA高可用搭建以及问题解决方案

    hadoop 集群HA高可用搭建 目录大纲 1. hadoop HA原理 2. hadoop HA特点 3. Zookeeper 配置 4. 安装Hadoop集群 5. Hadoop HA配置 搭建环 ...

  10. 虚拟机搭建Hadoop集群

    安装包准备 操作系统:ubuntu-16.04.3-desktop-amd64.iso 软件包:VirtualBox 安装包:hadoop-3.0.0.tar.gz,jdk-8u161-linux-x ...