实现Spring管理struts的Action
2016-11-27 20:17
363 查看
struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean。
可以在struts2中配置:
<struts>
<constant name="struts.objectFactory" value="spring" />
</struts>
同时action的配置class='beanID',访问该Action时,会通过class对应值去spring中寻找相同id值的bean。
也可以复制struts2-spring-plugin-x-x-x.jar到WEB-INF/lib目录下。
在struts2-spring-plugin-x-x-x.jar中有一个struts-plugin.xml配置文件,该配置文件配置内容:
<struts>
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<!-- Make the Spring object factory the automatic default -->
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.class.reloading.watchList" value="" />
<constant name="struts.class.reloading.acceptClasses" value="" />
<constant name="struts.class.reloading.reloadConfig" value="false" />
<package name="spring-default">
<interceptors>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
</interceptors>
</package>
</struts>
可以看出该spring插件的作用就是关联struts action和spring bean,在struts中就不必再配置 <constant name="struts.objectFactory" value="spring" />了。
1,action的class值应等于spring中的bean id值,实现关联,action由spring创建。
2,若查不到,则由该spring插件根据class的值创建action,并把该action赋给spring托管,在这种情况下action的创建不是由struts2完成,而是由插件完成,并且插件拥有把action纳入spring容器管理的功能,此时spring配置文件中可以不必配置action的bean了。
两种方式都使得action成为了spring中的一个bean实例。
所以,class既可以为beanId,也可以为类路径,当寻找到对应bean时可以直接作为bean访问,若寻找不到,则会由插件根据class类路径负责创建action实例并送给spring成为其管理的一个bean。
原文:http://blog.sina.com.cn/s/blog_4e345ce70101bszf.html
相关文章推荐
- 关于用spring来管理Struts中的action 所出现的问题
- Spring管理Struts Action
- 使用Spring的注解方式注入Struts所管理的action
- 如何使用Spring来管理Struts中的Action
- Spring管理Struts action的安全问题
- 如何使用Spring来管理Struts中的Action
- (上节)Spring+JDBC+Struts;实现图片上传;配置的属性默认dbc用单例模式,action一定要scope=“prototype”
- struts + mybatis +spring实现黑名单/灰名单管理
- 在struts.xml文件中使用spring代理,管理action
- 学习《JSP项目开发全程实录》中的企业人力资源管理系统(spring1.2+sturts1.2+hibernate3.0)的一些过程总结之管理员登录ACTION的实现
- 如何使用Spring来管理Struts中的Action
- Struts+Spring+Hibernate实现上传下载
- 成功实现了ajax--struts--spring无缝地结合构建
- Struts+Spring+Hibernate实现上传下载
- 用Struts2.0+Hibernate+Spring实现用户登陆
- Action 4 Spring Integrating Struts
- Struts+Spring+Hibernate实现上传下载
- Spring in Action 学习笔记—第五章事务管理
- struts+spring+hibernate实现数据库记分页显示
- 中国移动业务管理系统(struts+spring+hibernate)源码