日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 1790 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)論
主站蜘蛛池模板: 全椒县| 嘉禾县| 响水县| 大新县| 阿拉善右旗| 安徽省| 久治县| 建水县| 淮阳县| 彰化市| 吉木萨尔县| 岳西县| 双桥区| 长顺县| 万宁市| 巧家县| 静乐县| 石河子市| 静宁县| 平武县| 微博| 错那县| 湟中县| 苍山县| 霍山县| 聂荣县| 太仓市| 兴山县| 南安市| 石柱| 贵定县| 辽中县| 珲春市| 定远县| 平乐县| 油尖旺区| 四会市| 太仓市| 苍南县| 犍为县| 泰兴市|