博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
strut1.X和spring整合的二种方法
阅读量:6941 次
发布时间:2019-06-27

本文共 2805 字,大约阅读时间需要 9 分钟。

第一种集成方法

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

转载地址:http://dzinl.baihongyu.com/

你可能感兴趣的文章
Distributed Systems-Materials
查看>>
linux文件系统实现浅析
查看>>
linux系统中定义的信号
查看>>
大厂h5开源视频系列-网易云音乐年度总结
查看>>
Android-再次解读萤石云视频
查看>>
webpack 代码gzip压缩、注释、警告去除配置,设置缓存
查看>>
Android 实现自定义圆环
查看>>
Xcode实现彩色控制台
查看>>
macOS Sierra 10.12+ 软件提示已损坏,移除到废纸篓的解决方法
查看>>
Istio 庖丁解牛一:组件概览
查看>>
从0带您打造企业级 Vue 服务器渲染 Nuxt.js (一) 入门
查看>>
vue watch数组引发的血案
查看>>
LAMP和LNMP深度优化
查看>>
(三)Python的List和Tuple类型
查看>>
近期面试题整理
查看>>
1.编辑器 - Visual Studio Code
查看>>
MDKAutoLayoutHeight 自动化UITableviewCell高度计算工具
查看>>
JS 事件机制 Event Loop
查看>>
简历上的项目经历怎么写 ?这 3 条原则不可忽视 !
查看>>
Python:鲜为人知的功能特性(下)
查看>>