欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

低耦合的Struts集成Spring的實例

系統(tǒng) 1627 0

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

?

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規(guī)定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的實例


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精品久久久久久日本一道 | 日韩一区二区三区在线观看 | 免费国产精品视频在线 | 国产男女爽爽爽免费视频 | 国产乳摇福利视频在线观看 | 午夜a级片| 国产成人在线视频播放 | 91www成人久久 | 欧美一级欧美三级在线观看 | 新婚人妻不戴套国产精品 | 男女同床爽爽视频免费 | 99在线看 | 99久久久久久 | 亚洲欧美一区二区久久香蕉 | 色噜噜狠狠色综合日日 | 欧美激情一区二区亚洲专区 | 日日操夜夜草 | 精品视频在线观看视频免费视频 | 四虎影视在线影院在线观看观看 | 高清中文字幕视频在线播 | 久久com | 91国色 | 中国一级免费视频 | 国产传媒网址 | 97超级碰碰碰碰在线视频 | 成人精品鲁一区一区二区 | 福利色 | 九九精品九九 | 国产偷国产偷亚洲高清在线 | 欧美一级二级三级视频 | 国产精品a久久久久 | 天天色综合5 | 一区二区视频在线 | 2020国产精品视频免费 | 毛片基地看看成人免费 | 九九热免费视频在线观看 | 成年人在线视频网站 | 亚洲欧美日韩激情在线观看 | 欧美大片一区二区 | 国产亚洲精彩视频 | www国产成人免费观看视频,深夜成人网 |