MVC開放源碼中英文對照外文翻譯文獻_第1頁
MVC開放源碼中英文對照外文翻譯文獻_第2頁
MVC開放源碼中英文對照外文翻譯文獻_第3頁
MVC開放源碼中英文對照外文翻譯文獻_第4頁
MVC開放源碼中英文對照外文翻譯文獻_第5頁
已閱讀5頁,還剩10頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

畢業設計外文資料翻譯中英文對照外文翻譯文獻(文檔含英文原文和中文翻譯)原文: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的一種開放源碼實現本文介紹Struts,它是使用servlet和JavaServerPages技術的一種Model-View-Controller實現。Struts可幫助您控制Web項目中的變化并提高專業化水平。盡管您可能永遠不會用Struts實現一個系統,但您可以將其中的一些思想用于您以后的servlet和JSP網頁的實現中。模型-視圖-控制器(MVC)JSP標記只解決了部分問題。我們還得處理驗證、流程控制和更新應用程序的狀態等問題。這正是MVC發揮作用的地方。MVC通過將問題分為三個類別來幫助解決單一模塊方法所遇到的某些問題:Model(模型)

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

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

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

溫馨提示

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

評論

0/150

提交評論