spring+struts2整合
来源:互联网 发布:手机商品期货交易软件 编辑:程序博客网 时间:2024/06/05 15:00
如果让spring和struts2进行整合,我们就希望我们可以在spring中直接注入action,spring容器初始化的时候就给我们建好了action,但是事情不像我们想象的那么简单,因为struts2的action是由struts2自己new出来的,他不受spring的管理,所以无法自动注入。所以struts和spring的整合的结合点在于,struts2的action不能直接注入service。好了,既然有了问题,spring或者struts2肯定已经为我们把这个问题解决了。struts2解决这个问题是他给我们提供了一个struts-spring-plugin的插件,通过这个插件我们就可以把我们的struts2和是spring进行整合了。struts-spring-plugin将会对我们的action进行管理,当spring需要action的时候他就可以向struts-spring-plugin来要了。
下面我们就具体的来看一下struts+spring的整合过程:
1.需要的jar包列表:Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2
jar包名称
所在位置
说明
antlr-2.7.6.jar
hibernate/lib/required
解析HQL
aspectjrt
spring/lib/aspectj
AOP
aspectjweaver
..
AOP
cglib-nodep-2.1_3.jar
spring/lib/cglib
代理,二进制增强
common-annotations.jar
spring/lib/j2ee
@Resource
commons-collections-3.1.jar
hibernate/lib/required
集合框架
commons-fileupload-1.2.1.jar
struts/lib
struts
commons-io-1.3.2
struts/lib
struts
commons-logging-1.1.1
单独下载,删除1.0.4(struts/lib)
struts
spring
dom4j-1.6.1.jar
hibernate/required
解析xml
ejb3-persistence
hibernate-annotation/lib
@Entity
freemarker-2.3.13
struts/lib
struts
hibernate3.jar
hibernate
hibernate-annotations
hibernate-annotation/
hibernate-common-annotations
hibernate-annotation/lib
javassist-3.9.0.GA.jar
hiberante/lib/required
hibernate
jta-1.1.jar
..
hibernate transaction
junit4.5
mysql-
ognl-2.6.11.jar
struts/lib
slf4j-api-1.5.8.jar
hibernate/lib/required
hibernate-log
slf4j-nop-1.5.8.jar
hibernate/lib/required
spring.jar
spring/dist
struts2-core-2.1.6.jar
struts/lib
xwork-2.1.2.jar
struts/lib
struts2
commons-dbcp
spring/lib/jarkata-commons
commons-pool.jar
..
struts2-spring-plugin-2.1.6.jar
struts/lib
2.配置好jar包以后,如果我们想在服务器一启动就可以让spring容器自动去加载我们在配置文件中配置的bean,那么我们要在web.xml中去配置一个监听器,这个监听器的作用是监听我们的application,一旦我们的项目启动就触发了监听器,我们来看一下这个监听器的配置:
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- <!-- default: /WEB-INF/applicationContext.xml -->
- </listener>
如果你的配置文件不想放在默认的位置,而是自己去指定位置,那么我们将要在web.xml中再次配置如下:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- //这种配置可以指定多个配置文件,因为spring的配置文件可以分开写成好几个
- <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
- //指定spring配置文件的位置classpath下的beans.xml
- <param-value>classpath:beans.xml</param-value>
- </context-param>
加载完上面的bean之后,我们就要考虑去管理我们的action了,我们应该让spring去找struts去要相应的action,把action实例化为响应的bean,这时我们就需要我们上边所提到的struts-spring-plugin这个jar包了。加上这个jar包之后我们就可以让spring来管理我们的action了。在struts-spring-plugin中有一个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" />
- <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>
关键是这个 <constant name="struts.objectFactory" value="spring" />,这句配置就指明了我们产生struts对象的工厂交给spring来产生,我们来看一下具体步骤:
struts2一起动就会去加载配置文件,其中包括struts—plugin。xml读取顺序:
struts的读常量:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
struts-plugin.xml指明了我们产生对象的工厂交给spring来完成,当执行到web.xml时,由于spring容器的监听器,这时spring容器就开始启动,spring启动之后会web.xml去找相应的配置,在web.xml中可以找到spring中的配置文件beans.xml,然后去初始化所有的bean。
管理Action的两种方式:
1. struts负责管理Action
spring去加载beans.xml的时候会自动把所有的bean初始化,并且放在自己的容器里。与此同时,struts2也有自己的bean容器,这个容器是struts—plugin提供的,他会把所有的action加载都加载到自己的容器里。然后根据action的属性名字自动去spring去找名字和action属性相同的bean直接注入到action中,也就是说。我们在action中其实不用配置注入的东西,struts容器会帮我们自动把属性都注入。但还是要提供相应的set、get方法。并且名字要约定好,action属性和spring中的bean的id要一样。action的scope设置成prototype(默认也是prototype)。
下面我们来看一下具体的示例代码:
struts.xml配置如下:
<package name="student" namespace="/secure" extends="default">
<action name="bar" class="com.bzu.action.StudentAction">
<result>bar.ftl</result>
</action>
</package>
- package com.bzu.action;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.bzu.entity.Student;
- import com.bzu.service.StudentService;
- import com.opensymphony.xwork2.ActionSupport;
- public class StudentAction extends ActionSupport {
- private Student student;
- private StudentService service;
- // 默认为prototype
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- service.login(student);
- return SUCCESS;
- }
- public StudentService getService() {
- return service;
- }
- public Student getStudent() {
- return student;
- }
- public void setStudent(Student student) {
- this.student = student;
- }
- // 这里的service,struts容器会自动帮我们在spring中查找并注入。不需要我们写@Resource指定
- public void setService(StudentService service) {
- this.service = service;
- }
- }
需要注意的是,spring和struts2整合的时候有一个属性struts.objectFactory.spring.autoware,也就是说是struts属性的的自动装配类型,他的默认值是name,也就是说struts中的action中的属性不需要配置,他默认的去beans.xml中去找名字相同的,应该注意的是,在给一些属性起名字的时候不要和spring中配置的action的name属性相同,否则会报异常
2. spring负责产生并管理Action
struts.xml配置如下:
<package name="secure" namespace="/secure" extends="default">
<action name="bar" class="bar">
<result>bar.ftl</result>
</action>
</package>
要是把struts中action的class设置为spring容器中的bean的id时,action的创建及属性注入就由spring来管理了。
- Spring -- spring整合struts2
- Spring:Spring整合Struts2
- 【Spring】Spring整合Struts2
- EJB3,Spring,Struts2整合
- struts2+hibernate+spring整合
- Struts2 与 Spring 整合
- struts2 spring hibernate 整合
- Spring struts2 hibernate 整合
- struts2整合spring步骤
- spring/struts2整合
- spring 整合struts2
- spring + struts2 整合
- Struts2与Spring整合
- Struts2和Spring整合
- struts2+spring+hibernate整合
- Struts2+Spring整合
- struts2 spring整合注意事项。
- Struts2+Spring整合
- C++ 异常处理
- 第12周项目1--当年第几天
- Eclipse进行远程Debug代码
- UVa 10125 Sumsets (折半枚举&二分查找)
- jQuery验证控件jquery.validate.js使用说明+中文API
- spring+struts2整合
- 论“评博客”重要性
- 随机数
- 生活是如此的美妙
- [转]造成segment fault,产生core dump的可能原因
- VectorCAST-自动化软件测试工具
- java 数字计算精度问题
- 初识windows编程之动态链接库
- ExtJs ButtonGroup按钮组