这篇博客摘自[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. restful framework之频率组件

    一.频率简介 为了控制用户对某个url请求的频率,比如,一分钟以内,只能访问三次 二.自定义频率类.自定义频率规则 自定义的逻辑 #(1)取出访问者ip # (2)判断当前ip不在访问字典里,添加进去 ...

  2. gh-ost的延迟控制机制

    root@sbtest04:46:19>select * from _yougege_ghc limit 10\G*************************** 1. row ***** ...

  3. 使用Fiddler模拟客户端http响应【转】

    转自:使用Fiddler模拟客户端http响应 在客户端开发中,常常需要对一些特殊情况做处理,比如404.503等,又比如服务返回错误数据等.而测试这些情况会比较麻烦,往往都是找开发人员配合修改代码, ...

  4. 动态加载与插件系统的初步实现(二):AppDomain卸载与代理

    前一篇文章简单展示了类型发现和MEF使用,本文初步进入AppDomain相关内容. CLR程序运行时会创建默认程序集容器即AppDomain,默认AppDomain不支持卸载其程序集,但CLR支持创建 ...

  5. 利用workbench对linux/Ubuntu系统中的mysql数据库进行操作

    在上一篇文章中,我分享了在linux中如何安装mysql数据库,但是这只是安装了mysql的服务,并没有图形化管理界面,所以这样子操作起来并没有那么方便,那么现在我们就来实现如何利用在window中安 ...

  6. 小强版之无码理解C语言指针

     1. 先从普通变量开始   2. 撸完变量撸指针   3. 故事情节进一步发展,此处少儿不宜   4. 奶茶妹妹捉奸,小强死定了   5. 源码欣赏  #include <stdio.h> ...

  7. 基于神念TGAM的脑波小车(4)

    我使用的是HC05和BT06俩个蓝牙模块 1.[AT模式]HC05蓝牙模块的PIO11接VCC,上电后即进入HC05AT指令模式,对于BT06蓝牙直接上电进入AT模式,用USBT06转TTL模块连接到 ...

  8. DDMS_Threads的简单使用

    title: DDMS_Threads的简单使用 date: 2016-07-20 00:44:35 tags: [DDMS] categories: [Tool,IDE] --- 概述 本文记录在 ...

  9. (第二周)scrum站立会议

    敏捷流程scrum中的很重要的一个制度之一每日站立会议 了解的内容: 问题:为啥不用email汇报代替每日会议? E-mail不能取代每日会议,E-mail只会增加沟通成本,而且不能提供细节信息或者给 ...

  10. 学校网站UI设计分析

    在一个团队中PM,要更好的掌握项目的实施管理,包括对UI的设计,作为团队项目的PM,在听了老师课上的讲解后,对UI有了新的认识,对此,我对下面网站有了些自己的想法(只可意会不可言传,O(∩_∩)O~) ...