第一种集成方法

原理:在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象

缺点:产生了依赖,spring的类在action中产生了依赖查找。(注意和依赖注入的区别(前者主动))。

1、spring和struts依赖库配置

* 配置struts

--拷贝struts类库和jstl类库

--修改web.xml文件来配置ActionServlet

--提供struts-config.xml文件

--提供国际化资源文件

* 配置spring

--拷贝spring类库

--提供spring配置文件

2、在struts的Action中调用如下代码取得BeanFactory

BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());

使用listener配置beanfactory,将其初始化交给servlet,使其维持在ServletContext中,节省资源。(Listener初始化早于Servlet(Weblogic8除外))

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:applicationContext-*.xml</param-value>

</context-param>

<listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

3、通过BeanFactory取得业务对象,调用业务逻辑方法

补充:(Struts1.x相关并和Spring集成)

扩展学习:

l Jboss的jar包加载顺序(根据字母),因此可能使得有些包无法加载。

l 使用高级模板创建的jsp文件,由于有

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

------------

<base href="<%=basePath%>" />

因此,jsp中的目录都会从根目录下查找。

l Servlet Listener

Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:

ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。

ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。

HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。

HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

第二种集成方案

原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询

(客户端请求---->代理action--->取得beanFactory--->getBean(..)创建action示例--->执行exctute方法)

1、spring和struts依赖库配置

* 配置struts

--拷贝struts类库和jstl类库

--修改web.xml文件来配置ActionServlet

--提供struts-config.xml文件

--提供国际化资源文件

* 配置spring

--拷贝spring类库

--提供spring配置文件

2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来

3、在struts-config.xml文件中配置Action

* <action>标签中的type属性需要修改为

org.springframework.web.struts.DelegatingActionProxy

DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值

到IoC容器中取得本次请求对应的Action

4、在spring配置文件中需要定义struts的Action,如:

<bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">

<property name="userManager" ref="userManager"/>

</bean>

* 必须使用name属性,name属性值必须和struts-config.xml文件中<action>标签的path属性值一致

* 必须注入业务逻辑对象

* 建议将scope设置为prototype,这样就避免了struts Action的线程安全问题

strut1.X和spring整合的二种方法的更多相关文章

  1. spring整合mybatis二种配置

    第一种: <!-- 配置sqlsession --> <bean id="sqlsessionFactory" class="org.mybatis.s ...

  2. 使用Spring Security3的四种方法概述

    使用Spring Security3的四种方法概述 那么在Spring Security3的使用中,有4种方法: 一种是全部利用配置文件,将用户.权限.资源(url)硬编码在xml文件中,已经实现过, ...

  3. mysql 远程连接数据库的二种方法

    http://blog.csdn.net/freecodetor/article/details/5799550 一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5 ...

  4. IIS7.5使用web.config设置伪静态的二种方法

    转自 网上赚钱自学网 .http://www.whosmall.com/post/121 近几天公司里开发的项目有几个运行在IIS7.5上,由于全站采用的是伪静态,因此从网上找到两两种方法来实现.这两 ...

  5. mysql 远程连接数据库的二种方法

    一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口"3306",用户名为"root",密码"123 ...

  6. XML解析的二种方法之dom解析

    XML解析的二种方法:dom解析和sax解析 文件大小      存储位置          读取速度 dom解析     小文件      放在内存中              快 sax解析   ...

  7. debian7更换gcc版本的二种方法分享

    debian7更换gcc版本的二种方法分享   最近在编译qt,之前用的是debian6,gcc版本是gcc-4.4,当使用debian7时,编译遇到了很多跟debian6不一样的问题,debian7 ...

  8. SQL 2005批量插入数据的二种方法

    SQL 2005批量插入数据的二种方法 Posted on 2010-07-22 18:13 moss_tan_jun 阅读(2635) 评论(2) 编辑 收藏 在SQL Server 中插入一条数据 ...

  9. 转 mysql 远程连接数据库的二种方法

    mysql 远程连接数据库的二种方法   一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口“3306”,用户名为“root”,密码“123456” ...

随机推荐

  1. Struts2--课程笔记3

    获取ServletAPI: 第一种方式: //在request域中放入属性req,暂且认为getContext()获取的是request域空间,但实际不是        ActionContext.g ...

  2. pur-ftpd在ubuntu上的安装2(数据库管理)

    1.安装mysql数据库支持的pure-ftpd apt-get install pure-ftpd-mysql 2.添加分组"ftpgroup",并且添加分组虚拟用户" ...

  3. svn hooks的使用demo

    我是理论家: svn server端提供了Hooks Script.所谓钩子实际上是一种时间触发机制,是指当系统执行到某个特殊的事件时,触发我们预定义的动作,可以让我们在某些特定状态发生的时候做我们想 ...

  4. PHPExcel解决内存占用过大问题-dw 查找memoryCacheSize把1M改为2048M

    http://blog.sina.com.cn/s/blog_4ec7952d0101fcrd.html PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理E ...

  5. MyBatis和SpringMVC集成事务在Junit测试下有效但是在实际项目无效的问题

    一.问题说明 项目框架采用SSM,集成了事务回滚(方式见下),在单元测试的时候,测试事务是有效的,但是在实际项目上线的时候,却没有效果. 二.集成方式 application-mybatis.xml( ...

  6. hdu_3555_Bomb(数位DP)

    题目连接:hdu_3555_Bomb 题意:和2089一样,只是数据大了点,不过道理是一样的 #include<cstdio> #include<cstring> #defin ...

  7. Broken Keyboard(悲剧文本)

    你有一个键盘,键盘上所有的键都能正常使用,只是Home键和End键有时会自动按下.你并不知道这一情况,而是专心地打稿子,甚至连显示器都没开电源.当你打开显示器之后,展现在你面前的是一段悲剧文本.你的任 ...

  8. 一个Cmake的例子

    命令查询列表:http://www.cmake.org/cmake/help/v3.2/manual/cmake-commands.7.html # # Official dependency num ...

  9. LeetCode OJ 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  10. LeetCode OJ 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...