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

下載本文檔

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

文檔簡介

1、文章內(nèi)容來自Java私塾2013-12-27整合Spring與Struts2(java私塾)15.2  整合Spring與Struts215.2.1概述以上面的示例來說明整合Spring和Struts2的基本方式:· SampleAction與SampleService的生命周期和依賴關(guān)系都由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       由于現(xiàn)在和Spring結(jié)合了,可以由Spring為Action注入Action需要的SampleService的實例,也就是SampleAction引用SampleService的方式需要改變,其他的沒有變化,示例代碼如下: java代碼:查看復(fù)制到剪貼板打印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類型的屬性,并提供對應(yīng)的setter方法,這個setter方法是留給Spring注入對象實例的時候調(diào)用的,可以不用提供getter方法。       也就是

11、說,現(xiàn)在的SampleAction已經(jīng)不用知道邏輯層的具體實現(xiàn)了。15.2.4編寫Spring的配置文件applicationContext.xml       要讓Spring來管理SampleAction和SampleServiceImpl的實例,還需要新建一個Spring的配置文件。在src下新建一個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>  這個xml的根元素是<beans>,在<beans>中聲明了它的schema引用,除此之外,還有兩個

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

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

18、每次請求都會新建一個Action實例。l         <property>子元素。<property>元素的name屬性為service,代表SampleAction這個類有一個setter方法叫setSampleService;<property>元素的ref屬性為sampleService,代表Spring容器會將一個名為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實現(xiàn)了當(dāng)這個Web工程啟動的時候,就去讀取Spring的配置文件,這個類是由Spring提供的,這里只需要配置上去就可以了。上下文參數(shù)的配置里面,contextConfigLocation的值classpath*:applicationContext.xml,表明了所有出現(xiàn)在classpath路徑下的ap

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

溫馨提示

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

最新文檔

評論

0/150

提交評論