整合Spring与Struts2_第1页
整合Spring与Struts2_第2页
整合Spring与Struts2_第3页
整合Spring与Struts2_第4页
整合Spring与Struts2_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、文章内容来自Java私塾2013-12-27整合Spring与Struts2(java私塾)15.2  整合Spring与Struts215.2.1概述以上面的示例来说明整合Spring和Struts2的基本方式:· SampleAction与SampleService的生命周期和依赖关系都由Spring去管理。· Struts2需要SampleAction实例的时候,不是自己新建实例,而是向Spring去请求获取一个实例,也就是SampleAction实例的生命周期也由Spring来管理接下来就来具体看看怎么整合Spring与Struts2。15.2.

2、2拷入jar包要整合Spring和Struts2,需要先要拷入Spring需要的jar包,既包括Spring本身的jar包,也包括Struts2的Spring插件。       找到下载的Struts2的资源包中的lib文件夹,也就是struts-lib,将以下几个jar包拷入到我们的web工程的WEB-INFlib中:spring-beans-2.5.6.jar、spring-context-2.5.6.jar、spring-core-2.5.6.jar、spring-test-2.5.6.jar、spring-web

3、-2.5.6.jar、struts2-spring-plugin-.jar、commons-logging-1.0.4.jar、struts2-spring-plugin-.jar。15.2.3改写SampleAction       由于现在和Spring结合了,可以由Spring为Action注入Action需要的SampleService的实例,也就是SampleAction引用SampleService的方式需要改变,其他的没有变化,示例代码如下: java代码:查看复制到剪贴板打印1.

4、public class SampleAction extends ActionSupport  2.     /通过setter方式,由Spring来注入SampleService实例  3.     private SampleService service;    4.     public void setSe

5、rvice(SampleService service)   5.         this.service = service;  6.       7.     private String name;  8.     private Strin

6、g userId;  9.     public String getName()   10.         return name;  11.       12.     public void setName(String name)

7、   13.          = name;  14.       15.     public String getUserId()   16.         return userId

8、;  17.       18.     public void setUserId(String userId)   19.         this.userId = userId;  20.       21.   

9、;    22.     public String execute() throws Exception   23.         name = this.service.getNameById(userId);          24. 

10、60;       return SUCCESS;  25.       26.   在execute方法中不再直接新建SampleServiceImpl的实例了,而是声明了一个SampleSerivce类型的属性,并提供对应的setter方法,这个setter方法是留给Spring注入对象实例的时候调用的,可以不用提供getter方法。       也就是

11、说,现在的SampleAction已经不用知道逻辑层的具体实现了。15.2.4编写Spring的配置文件applicationContext.xml       要让Spring来管理SampleAction和SampleServiceImpl的实例,还需要新建一个Spring的配置文件。在src下新建一个applicationContext.xml文件,内容如下: java代码:查看复制到剪贴板打印1. <?xml version="1.0" encoding="UT

12、F-8"?>  2. <beans xmlns="/schema/beans"  3.         xmlns:xsi="/2001/XMLSchema-instance"  4.         xsi:sche

13、maLocation="  5.             /schema/beans   6. /schema/beans/spring-beans-2.5.xsd">  7.     <bean name=&q

14、uot;sampleService" class="cn.javass.spring.SampleServiceImpl"/>  8.       9.     <bean name="sampleAction" class="cn.javass.spring.SampleAction" scope="prototype">

15、60; 10.         <property name="service" ref="sampleService"/>  11.     </bean>  12. </beans>  这个xml的根元素是<beans>,在<beans>中声明了它的schema引用,除此之外,还有两个

16、<bean>元素,定义了由Spring管理的SampleServiceImpl和SampleAction。· 对于第一个<bean>元素来说l         name属性为它设置了一个名字l         class元素指定了它的实现类的全类名· 对于第二个<bean>元素来说l         n

17、ame属性和class属性的含义与第一个<bean>元素完全一样。l         scope属性,赋值为prototype(原型)。scope属性非常重要,它管理了注册在它里面的Bean的作用域。Spring容器默认的作用域是单例,即每次外界向Spring容器请求这个Bean,都是返回同一个实例;但是,Struts2的Action是需要在每次请求的时候,都要新建一个Action实例,所以,在配置对应Action的<bean>元素时,必须把它的scope属性赋值为prototype,以保证

18、每次请求都会新建一个Action实例。l         <property>子元素。<property>元素的name属性为service,代表SampleAction这个类有一个setter方法叫setSampleService;<property>元素的ref属性为sampleService,代表Spring容器会将一个名为sampleService的已经存在的Bean,注入给sampleAction的service属性。15.2.5在web.xml中引用Spring配置文

19、件       有了applicationContext之后,还要在Web环境下引用它,web.xml的示例为: java代码:查看复制到剪贴板打印1. <?xml version="1.0" encoding="UTF-8"?>  2. <web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="3.

20、      <context-param>  4.         <param-name>contextConfigLocation</param-name>  5.         <param-value>classpath*:applicationContext.xml</param-v

21、alue>  6.     </context-param>  7.         8.     <listener>  9.         <listener-class>  10.     

22、        org.springframework.web.context.ContextLoaderListener  11.         </listener-class>  12.     </listener>  13.       14.

23、     <filter>  15.         <filter-name>Struts2</filter-name>  16.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

24、  17.     </filter>  18.     <filter-mapping>  19.         <filter-name>Struts2</filter-name>  20.         <url-p

25、attern>/*</url-pattern>  21.     </filter-mapping>  22. </web-app>  listener实现了当这个Web工程启动的时候,就去读取Spring的配置文件,这个类是由Spring提供的,这里只需要配置上去就可以了。上下文参数的配置里面,contextConfigLocation的值classpath*:applicationContext.xml,表明了所有出现在classpath路径下的ap

26、plicationContext.xml文件,都是上面的这个Listener要读取的Spring配置文件。15.2.6修改struts.xml就快要大功告成了,最后一步,来修改struts.xml,需要做两件事:       首先,添加常量struts.objectFactory,其值为spring,这就指定了Struts2使用Action的时候并不是自己去新建,而是去向Spring请求获取Action的实例。示例如下: java代码:查看复制到剪贴板打印1. <constant name="struts.objectFactory" value="spring"/>

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论