低耦合的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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元