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

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

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

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

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

2) 在 web.xml 中使用 ContextLoaderListener

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>
  7. org.springframework.web.context.ContextLoaderListener
  8. </listener-class>
  9. </listener>

3) 在 web.xml 中使用 ContextLoaderServlet

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  4. </context-param>
  5. <servlet>
  6. <servlet-name>SpringContextServlet</servlet-name>
  7. <servlet-class>
  8. org.springframework.web.context.ContextLoaderServlet
  9. </servlet-class>
  10. <load-on-startup>1</load-on-startup>
  11. </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

  1. <form-beans>
  2. <form-bean name="loginForm" type="com.xxx.yyy.struts.LoginForm" />
  3. <!-- 省略很多-->
  4. </form-beans>
  5. <action-mappings>
  6. <action
  7. attribute="loginForm"
  8. name="loginForm"
  9. parameter="method"
  10. path="/login"
  11. scope="request"
  12. type="org.springframework.web.struts.DelegatingActionProxy">
  13. <forward name="login" path="/login.jsp"/>
  14. <forward name="main" path="/main.jsp"/>
  15. <!-- 省略很多-->
  1. </action>
  2. </action-mappings>

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

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

  1. <bean name="/login" class="com.xxx.yyy.struts.LoginAction">
  2. <property name="xxxService">
  3. <ref bean="xxxService"/>
  4. </property>
  5. <property name="yyyService">
  6. <ref bean="yyyService"/>
  7. </property>
  8. <property name="zzzService">
  9. <ref bean="zzzService"/>
  10. </property>
  11. </bean>

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

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

上面的 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. Kafka系列二 kafka相关问题理解

    1.kafka是什么 类JMS消息队列,结合JMS中的两种模式,可以有多个消费者主动拉取数据,在JMS中只有点对点模式才有消费者主动拉取数据. kafka是一个生产-消费模型. producer:生产 ...

  2. 腾讯x5webview集成实战

    应用中许多网页由于优化的不够理想,出现加载慢,加载时间长等,而且因为碎片化导致兼容性问题,有一些网页有视频内容,产品还提出各种小窗需求,搞得心力憔悴.找到公开的有crosswalk和x5webview ...

  3. vs2019编译redis

    版本信息 使用Redis源码版本,解压工程右键生成hiredis项目正常,编译Win32_Interop项目报下图错误(error C2039:system_error:不是std成员;error C ...

  4. Bitcoin区块链攻击方式

    目录 重放攻击-- 非人为攻击 其他攻击 重放攻击-- 非人为攻击 重放攻击 Replay Attach 攻击者重复发送相同的数据库包到目的主机,用以欺骗系统 用支付宝付款信息重复项商家索取商品 比特 ...

  5. 【SIKIA计划】_10_Unity5.1UI系统-UGUI笔记

    Canvas——TextEventSystem 事件系统 0.滚动文本列表(隐藏背景)/Scroll/maskimage[Scroll Rect][Mask]——text(拉伸到显示全部)Scroll ...

  6. javascript实现对html便签等字符的转义

    参考链接:https://www.jb51.net/article/152700.htm 请访问以上链接. 本人纯搬迁,防止原作者删除. <script> var HtmlUtil = { ...

  7. CF100015C

    主要找到环上任意一条边,有比较dis(u,v),dis(u,a)+w+dis(b,v),dis(u,b)+w+dis(a,u) 然后,然后没了 lca求dis(u,v):dis(u,v)=dis[u] ...

  8. 简介make命令和makefile文件

    一.为什么要用到 make 命令和 makefile 文件 在 Linux 下编写一个程序,每次编译都需要在命令行一行一行的敲命令.如果是一个很小的程序还好说,命令不怎的复杂,编译速度也挺快,但是对于 ...

  9. Django FBV/CBV、中间件、GIT使用

    s5day82 内容回顾: 1. Http请求本质 Django程序:socket服务端 a. 服务端监听IP和端口 c. 接受请求 \r\n\r\n:请求头和请求体 \r\n & reque ...

  10. Scrum立会报告+燃尽图(Final阶段第四次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...