亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

低耦合的Struts集成Spring的實例

系統 1560 0

我們在集成Spring和struts的時候,往往習慣于使用spring提供的ActionSupport,然后使用getWebApplicationContext()方法獲得spring的bean,這樣固然方便,但有一個弊端,就是我們的struts action依賴了spring的api,增加了耦合,現在什么都流行高內聚,低耦合,spring為我們提供了代理的Struts action,這樣,我們在struts-config.xml不再為path設置真正的action,而是設計spring的代理Action,然后由spring的代理action,去尋找在spring bean 容器中的真正的action,這樣,我們的action是一個完全沒有依賴于spring的action?,具體實現請看以下代碼:

?

Action:

?

/**/ /*
?*?Generated?by?MyEclipse?Struts
?*?Template?path:?templates/java/JavaClass.vtl
?
*/

package ?action;

import ?javax.servlet.http.HttpServletRequest;
import ?javax.servlet.http.HttpServletResponse;

import ?org.apache.struts.action.Action;
import ?org.apache.struts.action.ActionForm;
import ?org.apache.struts.action.ActionForward;
import ?org.apache.struts.action.ActionMapping;

import ?Service.StudentService;


public ? class ?ListStudentActionAction? extends ?Action? ... {
????
private ?StudentService?studentService;
????
public ?ActionForward?execute(ActionMapping?mapping,?ActionForm?form,
????????????HttpServletRequest?request,?HttpServletResponse?response)?
... {

????????System.out.println(
this .studentService.getStudent().getName() + " -- " + this .studentService.getStudent().getSex());
????????
return ? null ;
????}

????
// 通過spring注入service
???? public ? void ?setStudentService(StudentService?studentService)? ... {
????????
this .studentService? = ?studentService;
????}

????
????
}

?

Service:

?

package?Service;

import?Model.Student;

public?class?StudentService?{

??public?Student?getStudent(){
?????return?new?Student("name","sex");??
??}
}

?

applicationContext.xml

配置真正的strutsAction,并把service類注入

<? xml?version="1.0"?encoding="UTF-8" ?>
< beans
????
xmlns ="http://www.springframework.org/schema/beans"
????xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
????xsi:schemaLocation
="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >

< bean? name ="/listStudentAction" ?class ="action.ListStudentActionAction" >
??
< property? name ="studentService" >
????
< bean? class ="Service.StudentService" />
??
</ property >
</ bean >
</ beans >

?

struts-config.xml

?

<? xml?version="1.0"?encoding="UTF-8" ?>
<! DOCTYPE?struts-config?PUBLIC?"-//Apache?Software?Foundation//DTD?Struts?Configuration?1.2//EN"?"http://struts.apache.org/dtds/struts-config_1_2.dtd" >

< struts-config >
??
< data-sources? />
??
< form-beans? />
??
< global-exceptions? />
??
< global-forwards? />
??
< action-mappings? >
???
<!-- ?不再真正的action,而注冊spring的代理action
????<action?path="/listStudentAction"?type="action.ListStudentActionAction"?/>
??????
-->
????
< action? path ="/listStudentAction" ?type ="org.springframework.web.struts.DelegatingActionProxy" ? />


??
</ action-mappings >

???
< message-resources? parameter ="ApplicationResources" ? />


?

???
< plug-in? className ="org.springframework.web.struts.ContextLoaderPlugIn" >
????????
< set-property? property ="contextConfigLocation"
????????????value
="/WEB-INF/classes/applicationContext-service.xml" />
????
</ plug-in >
</ struts-config >

需要說明的是,由于spring dtd規定id不能有"/",所以我們用name定義path,并且,spring bean的name要和struts-config.xml中的path一致

使用DelegatingActionProxy的好處就在于你可以用不用任何spring特定的類編寫Struts Action,這個方法也有不足之處,就是不太直觀,因為所有路徑都映射到同一個類了

對于這種情況,spring也有解決方法,就是使用請求委托

首先,為struts-config.xml增加controller

?

?? <!-- ?使用請求委托? -->
?
< controller? processorClass ="org.springframework.web.struts.DelegatingRequestProcessor" >
?
</ controller >

?

然后,修改我們的path定義位 <action path="/listStudentAction" type="action.ListStudentActionAction"/>

這樣,又和我們單獨使用struts的時候一樣了,但內部還是讓spring取代理我們的真正的action

需要說明的是,這里的type其實是個擺設,完全可以使用 <action path="/listStudentAction"/>,寫上是為了解決我們上面提到的“不夠直觀的”的問題



低耦合的Struts集成Spring的實例


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 伊人色综合久久天天 | 女人l8毛片a一级毛片 | 91九色最新地址 | 欧美日韩中文字幕在线观看 | 欧美视频一二三区 | 国产精品一区二区三区免费 | 国产在线中文字幕 | 国产成人香蕉在线视频网站 | 久久久久久久久性潮 | 日本一区高清 | 蜜桃日本一道无卡不码高清 | 在线播放成人毛片免费视 | 成人久久伊人精品伊人 | 精品久久久久久国产免费了 | 欧美日韩毛片 | 香蕉久久综合精品首页 | 国产一区二区三区 | 九九色网 | 精品色| 日韩永久免费视频 | 热99精品 | 香蕉人精品视频多人免费永久视频 | 成人国产在线不卡视频 | 日韩成人免费在线视频 | 国产精品分类视频分类一区 | 亚洲无成人网77777 | 日韩一区精品视频在线看 | 国产91成人 | 国产成人精品免费视频大 | 国产欧美日韩精品综合 | 女性特黄一级毛片 | 四虎激情 | 亚洲精品乱码久久久久久 | 日本精品在线视频 | 成人a一级毛片免费看 | 狠狠色丁香婷婷久久综合考虑 | 欧美黑人激情性久久 | 亚洲欧洲日韩国产一区二区三区 | 免费国产小视频在线观看 | 一级国产精品一级国产精品片 | 国产不卡视频在线播放 |