日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

如何集成Spring和Struts(实例说明)

發(fā)布時間:2024/4/14 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何集成Spring和Struts(实例说明) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

1.Struts和Spring

Struts?代表了MVC第二類架構(gòu)的實現(xiàn),在Struts中最重要的組件是ActionServlet,Action和?ActionForm?子類,ActionServlet?代表controller,他基于配置文件接受請求和?把這些請求轉(zhuǎn)發(fā)到相應(yīng)的ActionForm和Action子類。?ActionForm把用戶輸入的數(shù)據(jù)傳送到Action,Action調(diào)用商務(wù)層組件完成必要的操作,最后提交到view。ActionServlet使用一個配置文件(struts-config.xml)加載Action子類的定義用以接受用戶請求,基于請求URL,?controller?找到一個action定義去接受這個請求,Struts構(gòu)件處理用戶請求,?檢查配置文件,?完成相應(yīng)的動作。

Spring是一種輕量級的容器,它使得使用一個外部XML配置文件非常容易綁定對象,每個對象能夠通過列出JavaBean屬性得到一個依賴?對象的指針,通過綁定XML配置文件使剩下的工作更加簡單。依賴注入(DI)是非常強大的功能,Spring支持可插拔的事務(wù)管理器,提供事物管理方式更?多的選擇.?它集成了持久性的架構(gòu)同時也提供了一個統(tǒng)一的exception?分類,Spring也提供面向方面(AOP)編程的簡單機制。

2.Struts和Spring的集成

將Struts應(yīng)用集成到Spring框架可以采用多種方法,首先Spring明顯地被設(shè)計用于解決JEE的現(xiàn)實問題,如復(fù)雜性,性能低下,可測試性及其他;第二,Spring框架包括一個AOP實現(xiàn)讓你可以使用面向方面的程序設(shè)計技術(shù);第三,?Spring?框架可以能夠非常容易地管理和協(xié)調(diào)Struts;和Struts類似,Spring也包含MVC?實現(xiàn),兩個架構(gòu)都有優(yōu)缺點,Struts是MVC最重要的架構(gòu),很多開發(fā)團隊學(xué)會了依靠Struts在規(guī)定時限內(nèi)開發(fā)出高質(zhì)量的軟件,因此開發(fā)團隊寧愿?集成Spring的功能也不愿意轉(zhuǎn)到Spring?MVC;好消息是Spring的結(jié)構(gòu)允許你集成Struts?Web?框架、基于Spring的業(yè)務(wù)層和持久層,我們的方法是應(yīng)用Spring中的ActionSupport類去集成Struts。

3.加載應(yīng)用的context

首先我們需要使用Spring中的ContextLoaderPlugin為Struts?ActionServlet去裝載Spring應(yīng)用的上下文,簡單在struts-config.xml?文件中增加plug-in,如下(1)所示:

﹤??xml?version="1.0"?encoding="ISO-8859-1"??﹥

﹤?!DOCTYPE?struts-config?PUBLIC

"-//Apache?Software?Foundation//DTD?Struts?Configuration?1.1//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"﹥

﹤?struts-config﹥

﹤?form-beans﹥

﹤?form-bean?name="searchForm"

type="org.apache.struts.validator.DynaValidatorForm"﹥

﹤?form-property?name="cardno"?type="java.lang.String"/﹥

﹤?/form-bean﹥

﹤?/form-beans﹥

﹤?global-forwards?type="org.apache.struts.action.ActionForward"﹥

﹤?forward?name="welcome"?path="/welcome.do"/﹥

﹤?forward?name="searchEntry"?path="/searchEntry.do"/﹥

﹤?forward?name="searchSubmit"?path="/searchSubmit.do"/﹥

﹤?/global-forwards﹥

﹤?action-mappings﹥

﹤?action?path="/welcome"?forward="/WEB-INF/pages/welcome.htm"/﹥

﹤?action?path="/searchEntry"?forward="/WEB-INF/pages/search.jsp"/﹥

﹤?action?path="/searchSubmit"

type="?com.infotek.Creditcard.actions.SearchSubmit"

input="/searchEntry.do"

validate="true"

name="searchForm"﹥

﹤?forward?name="success"?path="/WEB-INF/pages/detail.jsp"/﹥

﹤?forward?name="failure"?path="/WEB-INF/pages/search.jsp"/﹥

﹤?/action﹥

﹤?/action-mappings﹥

﹤?message-resources?parameter="ApplicationResources"/﹥

﹤?plug-in?className="org.apache.struts.validator.ValidatorPlugIn"﹥

﹤?set-property?property="pathnames"?value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/﹥

﹤?/plug-in﹥

﹤?plug-in?className="org.springframework.web.struts.ContextLoaderPlugIn"﹥?(1)

﹤?set-property?property="contextConfigLocation"?value="/WEB-INF/beans.xml"/﹥

﹤?/plug-in﹥

﹤?/struts-config﹥

4.使用Spring的ActionSupport類

要用Spring去集成Struts,創(chuàng)建一個Spring?上下文是必須要做的。?org.springframework.web.struts.ActionSupport?類提供一個?getWebApplicationContext()?方法非常容易地獲得Spring上下文,全部你需要去做的是從Spring的ActionSupport?代替Struts?中的Action類去延伸你的action,如下所示:

package?com.infotek.Creditcard.actions;

import?java.io.IOException;

import?javax.servlet.ServletException;

import?javax.servlet.http.HttpServletRequest;

import?javax.servlet.http.HttpServletResponse;

import?org.apache.struts.action.ActionError;

import?org.apache.struts.action.ActionErrors;

import?org.apache.struts.action.ActionForm;

import?org.apache.struts.action.ActionForward;

import?org.apache.struts.action.ActionMapping;

import?org.apache.struts.action.DynaActionForm;

import?org.springframework.context.ApplicationContext;

import?org.springframework.web.struts.ActionSupport;

import?com.?infotek.Creditcard.beans.Creditcard;

import?com.?infotek.Creditcard.business.CreditcardService;

public?class?SearchSubmit?extends?ActionSupport?{?|(1)

public?ActionForward?execute(ActionMapping?mapping,ActionForm?form,

HttpServletRequest?request,HttpServletResponse?response)

throws?IOException,?ServletException?{

DynaActionForm?searchForm?=?(DynaActionForm)?form;

String?isbn?=?(String)?searchForm.get("cardno");

//the?old?fashion?way

//CreditcardService?creditcardService?=?new?CreditcardServiceImpl();

ApplicationContext?ctx?=?getWebApplicationContext();?|(2)

CreditcardService?creditcardService?=

(CreditcardService?)?ctx.getBean("creditcardService");?|(3)

CreditCard?creditard?=?CreditCardService.read(cardno.trim());

if?(null?==?creditard)?{

ActionErrors?errors?=?new?ActionErrors();

errors.add(ActionErrors.GLOBAL_ERROR,new?ActionError?("message.notfound"));

saveErrors(request,?errors);

return?mapping.findForward("failure")?;

}

request.setAttribute("creditcard",?creditcard);

return?mapping.findForward("success");

}

}

在(1)中,我們通過延伸Spring?ActionSupport?類而不是Struts?Action?類創(chuàng)建了一個action;在(2)中,我們使用getWebApplicationContext()方法獲得一個?ApplicationContext;為了獲得商務(wù)服務(wù),?在(3)中,我們使用ApplicationContext去查找Spring?bean;這個技術(shù)非常容易理解,不幸的是它把Struts的action和Spring?framework綁定了,如果你想替換Spring你不得不重寫代碼,而且Struts的action不在Spring的控制之下,?遺憾的是這種方法無法獲得Spring?AOP的好處。

5.結(jié)論

e良師益友嘗試使用Spring的ActionSupport,ContextLoaderPlugIn去集成Struts,這是一種最高效的和最簡單的方式,另外還可用Spring中的代理子類去代理Struts中的RequestProcessor和代理Struts的actions。

轉(zhuǎn)載于:https://my.oschina.net/u/2600520/blog/598189

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的如何集成Spring和Struts(实例说明)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。