整合Spring與Struts2_第1頁(yè)
整合Spring與Struts2_第2頁(yè)
整合Spring與Struts2_第3頁(yè)
整合Spring與Struts2_第4頁(yè)
整合Spring與Struts2_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、文章內(nèi)容來(lái)自Java私塾2013-12-27整合Spring與Struts2(java私塾)15.2  整合Spring與Struts215.2.1概述以上面的示例來(lái)說(shuō)明整合Spring和Struts2的基本方式:· SampleAction與SampleService的生命周期和依賴關(guān)系都由Spring去管理。· Struts2需要SampleAction實(shí)例的時(shí)候,不是自己新建實(shí)例,而是向Spring去請(qǐng)求獲取一個(gè)實(shí)例,也就是SampleAction實(shí)例的生命周期也由Spring來(lái)管理接下來(lái)就來(lái)具體看看怎么整合Spring與Struts2。15.2.

2、2拷入jar包要整合Spring和Struts2,需要先要拷入Spring需要的jar包,既包括Spring本身的jar包,也包括Struts2的Spring插件。       找到下載的Struts2的資源包中的lib文件夾,也就是struts-lib,將以下幾個(gè)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       由于現(xiàn)在和Spring結(jié)合了,可以由Spring為Action注入Action需要的SampleService的實(shí)例,也就是SampleAction引用SampleService的方式需要改變,其他的沒(méi)有變化,示例代碼如下: java代碼:查看復(fù)制到剪貼板打印1.

4、public class SampleAction extends ActionSupport  2.     /通過(guò)setter方式,由Spring來(lái)注入SampleService實(shí)例  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的實(shí)例了,而是聲明了一個(gè)SampleSerivce類型的屬性,并提供對(duì)應(yīng)的setter方法,這個(gè)setter方法是留給Spring注入對(duì)象實(shí)例的時(shí)候調(diào)用的,可以不用提供getter方法。       也就是

11、說(shuō),現(xiàn)在的SampleAction已經(jīng)不用知道邏輯層的具體實(shí)現(xiàn)了。15.2.4編寫Spring的配置文件applicationContext.xml       要讓Spring來(lái)管理SampleAction和SampleServiceImpl的實(shí)例,還需要新建一個(gè)Spring的配置文件。在src下新建一個(gè)applicationContext.xml文件,內(nèi)容如下: java代碼:查看復(fù)制到剪貼板打印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>  這個(gè)xml的根元素是<beans>,在<beans>中聲明了它的schema引用,除此之外,還有兩個(gè)

16、<bean>元素,定義了由Spring管理的SampleServiceImpl和SampleAction。· 對(duì)于第一個(gè)<bean>元素來(lái)說(shuō)l         name屬性為它設(shè)置了一個(gè)名字l         class元素指定了它的實(shí)現(xiàn)類的全類名· 對(duì)于第二個(gè)<bean>元素來(lái)說(shuō)l         n

17、ame屬性和class屬性的含義與第一個(gè)<bean>元素完全一樣。l         scope屬性,賦值為prototype(原型)。scope屬性非常重要,它管理了注冊(cè)在它里面的Bean的作用域。Spring容器默認(rèn)的作用域是單例,即每次外界向Spring容器請(qǐng)求這個(gè)Bean,都是返回同一個(gè)實(shí)例;但是,Struts2的Action是需要在每次請(qǐng)求的時(shí)候,都要新建一個(gè)Action實(shí)例,所以,在配置對(duì)應(yīng)Action的<bean>元素時(shí),必須把它的scope屬性賦值為prototype,以保證

18、每次請(qǐng)求都會(huì)新建一個(gè)Action實(shí)例。l         <property>子元素。<property>元素的name屬性為service,代表SampleAction這個(gè)類有一個(gè)setter方法叫setSampleService;<property>元素的ref屬性為sampleService,代表Spring容器會(huì)將一個(gè)名為sampleService的已經(jīng)存在的Bean,注入給sampleAction的service屬性。15.2.5在web.xml中引用Spring配置文

19、件       有了applicationContext之后,還要在Web環(huán)境下引用它,web.xml的示例為: java代碼:查看復(fù)制到剪貼板打印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實(shí)現(xiàn)了當(dāng)這個(gè)Web工程啟動(dòng)的時(shí)候,就去讀取Spring的配置文件,這個(gè)類是由Spring提供的,這里只需要配置上去就可以了。上下文參數(shù)的配置里面,contextConfigLocation的值classpath*:applicationContext.xml,表明了所有出現(xiàn)在classpath路徑下的ap

26、plicationContext.xml文件,都是上面的這個(gè)Listener要讀取的Spring配置文件。15.2.6修改struts.xml就快要大功告成了,最后一步,來(lái)修改struts.xml,需要做兩件事:       首先,添加常量struts.objectFactory,其值為spring,這就指定了Struts2使用Action的時(shí)候并不是自己去新建,而是去向Spring請(qǐng)求獲取Action的實(shí)例。示例如下: java代碼:查看復(fù)制到剪貼板打印1. <constant name="struts.objectFactory" value="spring"/>

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論