MVC开放源码中英文对照外文翻译文献.docx
《MVC开放源码中英文对照外文翻译文献.docx》由会员分享,可在线阅读,更多相关《MVC开放源码中英文对照外文翻译文献.docx(9页珍藏版)》请在冰点文库上搜索。
MVC开放源码中英文对照外文翻译文献
中英文对照外文翻译文献
(文档含英文原文和中文翻译)
原文:
Struts——anopen-sourceMVCimplementation
ThisarticleintroducesStruts,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.
MVCModel2
TheWebbroughtsomeuniquechallengestosoftwaredevelopers,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.
Strutsdetails
TheActionServletclass
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.
TheActionFormclass
ActionFormmaintainsthesessionstatefortheWebapplication.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(控制器)
控制器对用户的输入作出反应。
它创建并设置模型。
MVCModel2
Web向软件开发人员提出了一些特有的挑战,最明显的就是客户机和服务器的无状态连接。
这种无状态行为使得模型很难将更改通知视图。
在Web上,为了发现对应用程序状态的修改,浏览器必须重新查询服务器。
另一个重大变化是实现视图所用的技术与实现模型或控制器的技术不同。
当然,我们可以使用Java(或者PERL、C/C++或别的语言)代码生成HTML。
这种方法有几个缺点:
∙Java程序员应该开发服务,而不是HTML。
∙更改布局时需要更改代码。
∙服务的用户应该能够创建网页来满足它们的特定需要。
∙网页设计人员不能直接参与网页开发。
∙嵌在代码中的HTML很难看。
∙对于Web,需要修改标准的MVC形式。
图4显示了MVC的Web改写版,通常也称为MVCModel2或MVC2。
详细分析Struts
ActionServlet类
您还记得函数映射的日子吗?
在那时,您会将某些输入事件映射到一个函数指针上。
如果您对此比较熟悉,您会将配置信息放入一个文件,并在运行时加载这个文件。
函数指针数组曾经是用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表单设置或更新的一般意义上的数据。
例如,您