strut1.X和spring整合的二种方法

第一种集成方法

原理:在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的线程安全问题

时间: 2024-08-10 03:24:17

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

Spring Security3的四种方法概述

使用Spring Security3的四种方法概述 那么在Spring Security3的使用中,有4种方法: 一种是全部利用配置文件,将用户.权限.资源(url)硬编码在xml文件中,已经实现过,并经过验证: 二种是用户和权限用数据库存储,而资源(url)和权限的对应采用硬编码配置,目前这种方式已经实现,并经过验证. 三种是细分角色和权限,并将用户.角色.权限和资源均采用数据库存储,并且自定义过滤器,代替原有的FilterSecurityInterceptor过滤器,     并分别实现Ac

使用Spring Security3的四种方法概述

使用Spring Security3的四种方法概述 那么在Spring Security3的使用中,有4种方法: 一种是全部利用配置文件,将用户.权限.资源(url)硬编码在xml文件中,已经实现过,并经过验证: 二种是用户和权限用数据库存储,而资源(url)和权限的对应采用硬编码配置,目前这种方式已经实现,并经过验证. 三种是细分角色和权限,并将用户.角色.权限和资源均采用数据库存储,并且自定义过滤器,代替原有的FilterSecurityInterceptor过滤器,     并分别实现Ac

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

debian7更换gcc版本的二种方法分享 最近在编译qt,之前用的是debian6,gcc版本是gcc-4.4,当使用debian7时,编译遇到了很多跟debian6不一样的问题,debian7的默认gcc使用的是gcc-4.7,可能是编译器版本的问题,所以需要将debian7的gcc版本更换为gcc-4.4,办法如下:(推荐用方法一) 方法一: 安装gcc4.4和g++4.4 复制代码 代码如下: sudo apt-get install gcc-4.4sudo apt-get isntal

PHP去掉数组重复值二种方法实例

PHP两种去掉数组重复值的方法,分别使用foreach方法和array_unique方法. 去除一个数组中的重复值,可以使用foreach方法,也可以使用array_unique方法. <?php $arrF = array(); $arrS = array(); $intTotal = 100; $intRand = 10; for($i=0; $i < $intTotal; $i++) { $arrF[] = rand(1, $intRand); $arrS[] = rand(1, $in

Python文件遍历二种方法

分享下有关Python文件遍历的两种方法,使用的OS模块的os.walk和os.listdir实现. 关于Python的文件遍历,大概有两种方法,一种是较为便利的os.walk(),还有一种是利用os.listdir()递归遍历.方法一:利用os.walkos.walk可以自顶向下或者自底向上遍历整个文件树,然后返回一个含有3个元素的tuple,(dirpath, dirnames, filenames).注意,os.walk()会返回一个generater,所以调用的时候一定要放到for循环中

linux系统下php安装mbstring扩展的二种方法

.执行 复制代码代码如下: yum install php-mbstring 2. 修改php.ini (这一步非常重要, 部分lxadmin版本无法自动修改) 复制代码代码如下: echo ‘extension=mbstring.so' >>/etc/php.ini #更具php安装目录而定 3. 重启web service 如果是apache: service httpd restart 方法二:php 5.36安装目录:/usr/local/php 复制代码代码如下: #cd /usr/

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

一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口"3306",用户名为"root",密码"123456" C:/>mysql -h 192.168.5.116 -P 3306 -u root -p123456 2.隐藏密码 如:MySQL 连接本地数据库,用户名为"root", C:/>mysql -h localhost -u root -p Enter pa

C#WinForm 直接导出DataGridView数据到Excel表格的二种方法对比

方法一.利用微软的excel 操作类 引用:using Excel = Microsoft.Office.Interop.Excel; 代码如下:         #region导出数据表:Excle (微软的excel 操作类)         ///<summary>         ///导出数据表:Excle         ///</summary>         ///<param name="myDGV"></param>

PHP生成二维码二种方法和实例

PHP生成二维码的两个方法和实例,分别使用Google API和PHP二维码生成类库PHP QR Code实现. 之前介绍过通过使用jQuery插件来生成二维码,今天分享下如何使用PHP生成二维码,以及如何生成中间带LOGO图像的二维码.利用Google API生成二维码Google提供了较为完善的二维码生成接口,调用API接口很简单,以下是调用代码: $urlToEncode="http://www.jbxue.com"; generateQRfromGoogle($urlToEnc