Spring與Struts集成開(kāi)發(fā)
最近喜歡將所學(xué)的東西理順一下,且發(fā)現(xiàn)寫(xiě)blog可以達(dá)成這目的。
那就來(lái)整理一下我對(duì)Spring與Struts集成開(kāi)發(fā)的一些想法。
首先確認(rèn)系統(tǒng)的結(jié)構(gòu)為三層的B/S模式結(jié)構(gòu),如下圖:

在圖中看出,Spring和Struts集成開(kāi)發(fā)中,Spring在業(yè)務(wù)邏輯層被使用(集成)。因?yàn)镾pring框架的依賴(lài)注入,AOP及可聲明的事務(wù)管理方面的技術(shù)優(yōu)勢(shì),使得用Spring來(lái)管理業(yè)務(wù)實(shí)體,實(shí)體之間的依賴(lài)關(guān)系,業(yè)務(wù)邏輯服務(wù)接口變得簡(jiǎn)單且可配置。至此我們要清楚:Spring和Struts集成開(kāi)發(fā)中,Spring在業(yè)務(wù)邏輯層被使用(集成)。
清楚Struts和Spring在系統(tǒng)結(jié)構(gòu)中分別充當(dāng)?shù)慕巧螅酉聛?lái)要討論:如何實(shí)現(xiàn)集成?
1、使用Spring的ActionSurpert類(lèi)集成Struts。
org.springframework.web.struts.ActionSurpert是一個(gè)繼承org.apache.struts.action.Action的類(lèi),簡(jiǎn)要代碼如下:
public abstract class ActionSurpert extends Action {
private WebApplicationContext webApplicationContext;
public void setServlet(ActionServlet actionServlet) {//當(dāng)容器實(shí)例化此Action時(shí)被容器調(diào)用
surper.setServlet(actionServlet);
if(actionServlet != null) {
this.webApplicationContext = initWebApplicationContext(actionServlet);//獲取webApplicationContext
//........
}else{
//.......
}
}
//通過(guò)getXXX()方法獲取 webApplicationContext 對(duì)象
protected final WebApplictionContext getWebApplicationContext() {
return this.webApplicationContext;
}
}
通過(guò)上述代碼可以看出,所有繼承了ActionSupport類(lèi)的Action將可以通過(guò)WebApplicationContext對(duì)象的getBean(beanKey)方法獲得Spring配置文件中定義的各種Bean。
WebApplicationContext要由Web Server來(lái)加載,有兩種方法:
1、通過(guò)org.springframework.web.struts.ContextLoaderPlugIn加載,ContextLoaderPlugIn是個(gè)插件,需要在Struts配置文件中配置。
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property = "contextConfigLocation" value="/WEB-INF/applicationContext.xml"></set-property>
</plug-in>
2、在web.xml文件中加載
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
舉個(gè)簡(jiǎn)單的例子說(shuō)明一下相關(guān)配置信息:
假定有ExampleAction,ExampleBean,ExampleService這幾個(gè)類(lèi),它們工作流程是:
用戶(hù)請(qǐng)求ExampleAction,ExampleAction調(diào)用ExampleService的接口方法對(duì)ExampleBean進(jìn)行相關(guān)的操作。
1、applicationContext.xml中配置相關(guān)的bean信息
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="exampleBean" class="org.mypackge.beans.ExampleBean"/>
<bean id="exampleBeanService" class="org.mypackge.services.ExampleService"/>@
</beans>
通過(guò)這樣配置后,在ExampleAction中可以用getWebApplicationContext() 獲得webApplicationContext對(duì)象,然后通過(guò)
webApplicationContext的getBean(beanKey)方法獲得相應(yīng)的bean進(jìn)行業(yè)務(wù)處理。標(biāo)了紅色的"beanKey"就是applicationContext.xml中<bean>元素定義的bean id,如:webApplicationContext.getBean("exampleBeanService")。@
當(dāng)然,ExampleAction還要在stuts-config.xml配置文件中配置,這里不作介紹。
2、使用Spring的Action代理集成Struts
這種集成方式的核心思想是,將Struts的配置文件中的所有Action的type屬性設(shè)為org.springframwork.web.struts.DelegationActionProxy。當(dāng)用戶(hù)請(qǐng)求Action時(shí),就執(zhí)行這代理,代理會(huì)在Spring應(yīng)用上下文中找到真正的Action,然后交給它處理用戶(hù)的請(qǐng)求。而真正用于處理用戶(hù)請(qǐng)求的Action的配置放在了Spring的配置文件中。這樣,Struts中的Action以及其依賴(lài)關(guān)系就可以用Spring容器來(lái)管理,比如將業(yè)務(wù)邏輯對(duì)象注入到Action中,供其使用。簡(jiǎn)單片段<bean name="/exampleAction" class="org.myproj.struts.actions.ExampleAction">
....
</bean>
說(shuō)明:用name屬性而不是用id來(lái)標(biāo)識(shí)這個(gè)Bean,Spring不允許ID中出現(xiàn)"/",而name可以;"name"屬性值要和struts-config.xml文件中相應(yīng)<action>元素中的path屬性值相同(<action path="/exampleAction"),這樣Action代理才能找到相應(yīng)的Action來(lái)處理請(qǐng)求。
歡迎討論,提出寶貴意見(jiàn)。
posted on 2008-03-01 11:08
Sonny Li 閱讀(820)
評(píng)論(2) 編輯 收藏 所屬分類(lèi):
框架相關(guān)