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

低耦合的Struts集成Spring的實(shí)例

系統(tǒng) 1785 0

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

?

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 ;
????}

????
// 通過(guò)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,而注冊(cè)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 >

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

使用DelegatingActionProxy的好處就在于你可以用不用任何spring特定的類編寫Struts Action,這個(gè)方法也有不足之處,就是不太直觀,因?yàn)樗新窂蕉加成涞酵粋€(gè)類了

對(duì)于這種情況,spring也有解決方法,就是使用請(qǐng)求委托

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

?

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

?

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

這樣,又和我們單獨(dú)使用struts的時(shí)候一樣了,但內(nèi)部還是讓spring取代理我們的真正的action

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



低耦合的Struts集成Spring的實(shí)例


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 色综合婷婷 | 99久久国产综合精品成人影院 | 久久天天躁狠狠躁夜夜中文字幕 | 久久久国产精品免费看 | 天天色狠狠干 | 中文字幕一区二区三区有限公司 | 精品不卡一区中文字幕 | 久久久精品免费热线观看 | 免费看又爽又黄禁片视频1000 | 午夜亚洲国产精品福利 | 日本免费成人网 | 日本中文字幕不卡免费视频 | 久久一er精这里有精品 | 99久久精品免费看国产高清 | 国产l精品国产亚洲区在线观看 | 成人短视频在线观看 | 在线观看深夜观看网站免费 | 亚洲天堂三级 | 老湿机午夜影院 | 99久久99久久精品免费看蜜桃 | 中文字幕在线国产 | 男女性高清爱潮视频免费观看 | 黄色影院7755 | 日本成人不卡 | 久久88| 亚洲成a人片在线观看www | 欧美日韩亚毛片免费观看 | 精品视频在线免费播放 | 欧美日韩一区二区高清免费视频 | 久久永久影院免费 | 天天拍拍天天爽免费视频 | 亚洲日本中文字幕在线 | 四虎永久免费 | 一区毛片| 久久黄色小视频 | 天天干天天干天天干天天干天天干 | 一区二区三区精品国产 | 成人亚洲精品一区 | 伊人网在线播放 | 免费视频一级片 | 五月免费视频 |