MVC開放源碼中英文對照外文翻譯文獻(xiàn)_第1頁
MVC開放源碼中英文對照外文翻譯文獻(xiàn)_第2頁
MVC開放源碼中英文對照外文翻譯文獻(xiàn)_第3頁
MVC開放源碼中英文對照外文翻譯文獻(xiàn)_第4頁
MVC開放源碼中英文對照外文翻譯文獻(xiàn)_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

畢業(yè)設(shè)計(jì)外文資料翻譯中英文對照外文翻譯文獻(xiàn)(文檔含英文原文和中文翻譯)原文:Struts——anopen-sourceMVCimplementationThisarticleintroducesStruts,aModel-View-ControllerimplementationthatusesservletsandJavaServerPages(JSP)technology.StrutscanhelpyoucontrolchangeinyourWebprojectandpromotespecialization.EvenifyouneverimplementasystemwithStruts,youmaygetsomeideasforyourfutureservletsandJSPpageimplementation.Model-View-Controller(MVC)JSPtagssolvedonlypartofourproblem.Westillhaveissueswithvalidation,flowcontrol,andupdatingthestateoftheapplication.ThisiswhereMVCcomestotherescue.MVChelpsresolvesomeoftheissueswiththesinglemoduleapproachbydividingtheproblemintothreecategories:Model

Themodelcontainsthecoreoftheapplication'sfunctionality.Themodelencapsulatesthestateoftheapplication.Sometimestheonlyfunctionalityitcontainsisstate.Itknowsnothingaboutthevieworcontroller.View

Theviewprovidesthepresentationofthemodel.Itisthelookoftheapplication.Theviewcanaccessthemodelgetters,butithasnoknowledgeofthesetters.Inaddition,itknowsnothingaboutthecontroller.Theviewshouldbenotifiedwhenchangestothemodeloccur.Controller

Thecontrollerreactstotheuserinput.Itcreatesandsetsthemodel.MVCModel2TheWebbroughtsomeuniquechallengestosoftwaredevelopers,mostnotablythestatelessconnectionbetweentheclientandtheserver.Thisstatelessbehaviormadeitdifficultforthemodeltonotifytheviewofchanges.OntheWeb,thebrowserhastore-querytheservertodiscovermodificationtothestateoftheapplication.Anothernoticeablechangeisthattheviewusesdifferenttechnologyforimplementationthanthemodelorcontroller.Ofcourse,wecoulduseJava(orPERL,C/C++orwhatever)codetogenerateHTML.Thereareseveraldisadvantagestothatapproach:Javaprogrammersshoulddevelopservices,notHTML.Changestolayoutwouldrequirechangestocode.Customersoftheserviceshouldbeabletocreatepagestomeettheirspecificneeds.Thepagedesignerisn'tabletohavedirectinvolvementinpagedevelopment.HTMLembeddedintocodeisugly.FortheWeb,theclassicalformofMVCneededtochange.Figure4displaystheWebadaptationofMVC,alsocommonlyknownasMVCModel2orMVC2.StrutsdetailsTheActionServletclass

Doyourememberthedaysoffunctionmappings?Youwouldmapsomeinputeventtoapointertoafunction.Ifyouwhereslick,youwouldplacetheconfigurationinformationintoafileandloadthefileatruntime.FunctionpointerarrayswerethegoodolddaysofstructuredprogramminginC.LifeisbetternowthatwehaveJavatechnology,XML,J2EE,andallthat.TheStrutsControllerisaservletthatmapsevents(aneventgenerallybeinganHTTPpost)toclasses.Andguesswhat--theControllerusesaconfigurationfilesoyoudon_thavetohard-codethevalues.Lifechanges,butstaysthesame.ActionServletistheCommandpartoftheMVCimplementationandisthecoreoftheFramework.ActionServlet(Command)createsandusesAction,anActionForm,andActionForward.Asmentionedearlier,thestruts-config.xmlfileconfigurestheCommand.DuringthecreationoftheWebproject,ActionandActionFormareextendedtosolvethespecificproblemspace.Thefilestruts-config.xmlinstructsActionServletonhowtousetheextendedclasses.Thereareseveraladvantagestothisapproach:Theentirelogicalflowoftheapplicationisinahierarchicaltextfile.Thismakesiteasiertoviewandunderstand,especiallywithlargeapplications.ThepagedesignerdoesnothavetowadethroughJavacodetounderstandtheflowoftheapplication.TheJavadeveloperdoesnotneedtorecompilecodewhenmakingflowchanges.CommandfunctionalitycanbeaddedbyextendingActionServlet.TheActionFormclassActionFormmaintainsthesessionstatefortheWebapplication.ActionFormisanabstractclassthatissub-classedforeachinputformmodel.WhenIsayinputformmodel,IamsayingActionFormrepresentsageneralconceptofdatathatissetorupdatedbyaHTMLform.Forinstance,youmayhaveaUserActionFormthatissetbyanHTMLForm.TheStrutsframeworkwill:ChecktoseeifaUserActionFormexists;ifnot,itwillcreateaninstanceoftheclass.StrutswillsetthestateoftheUserActionFormusingcorrespondingfieldsfromtheHttpServletRequest.Nomoredreadfulrequest.getParameter()calls.Forinstance,theStrutsframeworkwilltakefnamefromrequeststreamandcallUserActionForm.setFname().TheStrutsframeworkupdatesthestateoftheUserActionFormbeforepassingittothebusinesswrapperUserAction.BeforepassingittotheActionclass,Strutswillalsoconductformstatevalidationbycallingthevalidation()methodonUserActionForm.Note:Thisisnotalwayswisetodo.TheremightbewaysofusingUserActionForminotherpagesorbusinessobjects,wherethevalidationmightbedifferent.ValidationofthestatemightbebetterintheUserActionclass.TheUserActionFormcanbemaintainedatasessionlevel.Notes:Thestruts-config.xmlfilecontrolswhichHTMLformrequestmapstowhichActionForm.MultiplerequestscanbemappedUserActionForm.UserActionFormcanbemappedovermultiplepagesforthingssuchaswizards.TheActionclass

TheActionclassisawrapperaroundthebusinesslogic.ThepurposeofActionclassistotranslatetheHttpServletRequesttothebusinesslogic.TouseAction,subclassandoverwritetheprocess()method.TheActionServlet(Command)passestheparameterizedclassestoActionFormusingtheperform()method.Again,nomoredreadfulrequest.getParameter()calls.Bythetimetheeventgetshere,theinputformdata(orHTMLformdata)hasalreadybeentranslatedoutoftherequeststreamandintoanActionFormclass.Note:"Thinkthin"whenextendingtheActionclass.TheActionclassshouldcontroltheflowandnotthelogicoftheapplication.ByplacingthebusinesslogicinaseparatepackageorEJB,weallowflexibilityandreuse.AnotherwayofthinkingaboutActionclassisastheAdapterdesignpattern.ThepurposeoftheActionisto"Converttheinterfaceofaclassintoanotherinterfacetheclientsexpect.Adapterletsclassesworktogetherthatcouldn_totherwisebecauseofincompatibilityinterface"(fromDesignPatterns-ElementsofReusableOOSoftwarebyGof).TheclientinthisinstanceistheActionServletthatknowsnothingaboutourspecificbusinessclassinterface.Therefore,Strutsprovidesabusinessinterfaceitdoesunderstand,Action.ByextendingtheAction,wemakeourbusinessinterfacecompatiblewithStrutsbusinessinterface.(AninterestingobservationisthatActionisaclassandnotaninterface.Actionstartedasaninterfaceandchangedintoaclassovertime.Nothing'sperfect.)TheErrorclasses

TheUMLdiagram(Figure6)alsoincludedActionErrorandActionErrors.ActionErrorencapsulatesanindividualerrormessage.ActionErrorsisacontainerofActionErrorclassesthattheViewcanaccessusingtags.ActionErrorsisStrutswayofkeepingupwithalistoferrors.TheActionMappingclass

AnincomingeventisnormallyintheformofanHTTPrequest,whichtheservletContainerturnsintoanHttpServletRequest.TheControllerlooksattheincomingeventanddispatchestherequesttoanActionclass.Thestruts-config.xmldetermineswhatActionclasstheControllercalls.Thestruts-config.xmlconfigurationinformationistranslatedintoasetofActionMapping,whichareputintocontainerofActionMappings.(Ifyouhavenotnoticedit,classesthatendwithsarecontainers)TheActionMappingcontainstheknowledgeofhowaspecificeventmapstospecificActions.TheActionServlet(Command)passestheActionMappingtotheActionclassviatheperform()method.ThisallowsActiontoaccesstheinformationtocontrolflow.ActionMappings

ActionMappingsisacollectionofActionMappingobjects.譯文:Struts——MVC的一種開放源碼實(shí)現(xiàn)本文介紹Struts,它是使用servlet和JavaServerPages技術(shù)的一種Model-View-Controller實(shí)現(xiàn)。Struts可幫助您控制Web項(xiàng)目中的變化并提高專業(yè)化水平。盡管您可能永遠(yuǎn)不會用Struts實(shí)現(xiàn)一個(gè)系統(tǒng),但您可以將其中的一些思想用于您以后的servlet和JSP網(wǎng)頁的實(shí)現(xiàn)中。模型-視圖-控制器(MVC)JSP標(biāo)記只解決了部分問題。我們還得處理驗(yàn)證、流程控制和更新應(yīng)用程序的狀態(tài)等問題。這正是MVC發(fā)揮作用的地方。MVC通過將問題分為三個(gè)類別來幫助解決單一模塊方法所遇到的某些問題:Model(模型)

模型包含應(yīng)用程序的核心功能。模型封裝了應(yīng)用程序的狀態(tài)。有時(shí)它包含的唯一功能就是狀態(tài)。它對視圖或控制器一無所知。View(視圖)

視圖提供模型的表示。它是應(yīng)用程序的外觀。視圖可以訪問模型的讀方法,但不能訪問寫方法。此外,它對控制器一無所知。當(dāng)更改模型時(shí),視圖應(yīng)得到通知。Controller(控制器)

控制器對用戶的輸入作出反應(yīng)。它創(chuàng)建并設(shè)置模型。MVCModel2Web向軟件開發(fā)人員提出了一些特有的挑戰(zhàn),最明顯的就是客戶機(jī)和服務(wù)器的無狀態(tài)連接。這種無狀態(tài)行為使得模型很難將更改通知視圖。在Web上,為了發(fā)現(xiàn)對應(yīng)用程序狀態(tài)的修改,瀏覽器必須重新查詢服務(wù)器。另一個(gè)重大變化是實(shí)現(xiàn)視圖所用的技術(shù)與實(shí)現(xiàn)模型或控制器的技術(shù)不同。當(dāng)然,我們可以使用Java(或者PERL、C/C++或別的語言)代碼生成HTML。這種方法有幾個(gè)缺點(diǎn):Java程序員應(yīng)該開發(fā)服務(wù),而不是HTML。更改布局時(shí)需要更改代碼。服務(wù)的用戶應(yīng)該能夠創(chuàng)建網(wǎng)頁來滿足它們的特定需要。網(wǎng)頁設(shè)計(jì)人員不能直接參與網(wǎng)頁開發(fā)。嵌在代碼中的HTML很難看。對于Web,需要修改標(biāo)準(zhǔn)的MVC形式。圖4顯示了MVC的Web改寫版,通常也稱為MVCModel2或MVC2。詳細(xì)分析StrutsActionServlet類您還記得函數(shù)映射的日子嗎?在那時(shí),您會將某些輸入事件映射到一個(gè)函數(shù)指針上。如果您對此比較熟悉,您會將配置信息放入一個(gè)文件,并在運(yùn)行時(shí)加載這個(gè)文件。函數(shù)指針數(shù)組曾經(jīng)是用C語言進(jìn)行結(jié)構(gòu)化編程的很好方法?,F(xiàn)在好多了,我們有了Java技術(shù)、XML、J2EE,等等。Struts的控制器是將事件(事件通常是HTTPpost)映射到類的一個(gè)servlet。正如您所料--控制器使用配置文件以使您不必對這些值進(jìn)行硬編碼。時(shí)代變了,但方法依舊。ActionServlet是該MVC實(shí)現(xiàn)的Command部分,它是這一框架的核心。ActionServlet(Command)創(chuàng)建并使用Action、ActionForm和ActionForward。如前所述,struts-config.xml文件配置該Command。在創(chuàng)建Web項(xiàng)目時(shí),您將擴(kuò)展Action和ActionForm來解決特定的問題。文件struts-config.xml指示ActionServlet如何使用這些擴(kuò)展的類。這種方法有幾個(gè)優(yōu)點(diǎn):應(yīng)用程序的整個(gè)邏輯流程都存儲在一個(gè)分層的文本文件中。這使得人們更容易查看和理解它,尤其是對于大型應(yīng)用程序而言。網(wǎng)頁設(shè)計(jì)人員不必費(fèi)力地閱讀Java代碼來理解應(yīng)用程序的流程。Java開發(fā)人員也不必在更改流程以后重新編譯代碼??梢酝ㄟ^擴(kuò)展ActionServlet來添加Command功能。ActionForm類ActionForm維護(hù)Web應(yīng)用程序的會話狀態(tài)。ActionForm是一個(gè)抽象類,必須為每個(gè)輸入表單模型創(chuàng)建該類的子類。當(dāng)我說輸入表單模型時(shí),是指ActionForm表示的是由HTML表單設(shè)置或更新的一般意義上的數(shù)據(jù)。例如,您可能有一個(gè)由HTML表單設(shè)置的UserActionForm。Struts框架將執(zhí)行以下操作:檢查UserActionForm是否存在;如果不存在,它將創(chuàng)建該類的一個(gè)實(shí)例。Struts將使用HttpServletRequest中相應(yīng)的域設(shè)置UserActionForm的狀態(tài)。沒有太多討厭的request.getParameter()調(diào)用。例如,Struts框架將從請求流中提取fname,并調(diào)用UserActionForm.setFname()。Struts框架在將UserActionForm傳遞給業(yè)務(wù)包裝UserAction之前將更新它的狀態(tài)。在將它傳遞給Action類之前,Struts還會對UserActionForm調(diào)用validation()方法進(jìn)行表單狀態(tài)驗(yàn)證。注:這并不總是明智之舉。別的網(wǎng)頁或業(yè)務(wù)可能使用UserActionForm,在這些地方,驗(yàn)證可能有所不同。在UserAction類中進(jìn)行狀態(tài)驗(yàn)證可能更好??稍跁捈壘S護(hù)UserActionForm。注:struts-config.xml文件控制HTML表單請求與ActionForm之間的映射關(guān)系??蓪⒍鄠€(gè)請求映射到UserActionForm。UserActionForm可跨多頁進(jìn)行映射,以執(zhí)行諸如向?qū)е惖牟僮?。Action類Action類是業(yè)務(wù)邏輯的一個(gè)包裝。Action類的用途是將HttpServletRequest轉(zhuǎn)換為業(yè)務(wù)邏輯。要使用Action,請創(chuàng)建它的子類并覆蓋process()方法。ActionServlet(Command)使用perform()方法將參數(shù)化的類傳遞給Ac

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論