MVC外文翻译(C4-1).doc

上传人:聆听****声音 文档编号:1892081 上传时间:2023-05-02 格式:DOC 页数:20 大小:199.50KB
下载 相关 举报
MVC外文翻译(C4-1).doc_第1页
第1页 / 共20页
MVC外文翻译(C4-1).doc_第2页
第2页 / 共20页
MVC外文翻译(C4-1).doc_第3页
第3页 / 共20页
MVC外文翻译(C4-1).doc_第4页
第4页 / 共20页
MVC外文翻译(C4-1).doc_第5页
第5页 / 共20页
MVC外文翻译(C4-1).doc_第6页
第6页 / 共20页
MVC外文翻译(C4-1).doc_第7页
第7页 / 共20页
MVC外文翻译(C4-1).doc_第8页
第8页 / 共20页
MVC外文翻译(C4-1).doc_第9页
第9页 / 共20页
MVC外文翻译(C4-1).doc_第10页
第10页 / 共20页
MVC外文翻译(C4-1).doc_第11页
第11页 / 共20页
MVC外文翻译(C4-1).doc_第12页
第12页 / 共20页
MVC外文翻译(C4-1).doc_第13页
第13页 / 共20页
MVC外文翻译(C4-1).doc_第14页
第14页 / 共20页
MVC外文翻译(C4-1).doc_第15页
第15页 / 共20页
MVC外文翻译(C4-1).doc_第16页
第16页 / 共20页
MVC外文翻译(C4-1).doc_第17页
第17页 / 共20页
MVC外文翻译(C4-1).doc_第18页
第18页 / 共20页
MVC外文翻译(C4-1).doc_第19页
第19页 / 共20页
MVC外文翻译(C4-1).doc_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

MVC外文翻译(C4-1).doc

《MVC外文翻译(C4-1).doc》由会员分享,可在线阅读,更多相关《MVC外文翻译(C4-1).doc(20页珍藏版)》请在冰点文库上搜索。

MVC外文翻译(C4-1).doc

Struts——anopen-sourceMVCimplementation

ThisarticleintroducesStruts,aModel-View-ControllerimplementationthatusesservletsandJavaServerPages(JSP)technology.StrutscanhelpyoucontrolchangeinyourWebprojectandpromotespecialization.EvenifyouneverimplementasystemwithStruts,youmaygetsomeideasforyourfutureservletsandJSPpageimplementation.

Introduction

KidsingradeschoolputHTMLpagesontheInternet.However,thereisamonumentaldifferencebetweenagradeschoolpageandaprofessionallydevelopedWebsite.Thepagedesigner(orHTMLdeveloper)mustunderstandcolors,thecustomer,productflow,pagelayout,browsercompatibility,imagecreation,JavaScript,andmore.Puttingagreatlookingsitetogethertakesalotofwork,andmostJavadevelopersaremoreinterestedincreatingagreatlookingobjectinterfacethanauserinterface.JavaServerPages(JSP)technologyprovidesthegluebetweenthepagedesignerandtheJavadeveloper.

Ifyouhaveworkedonalarge-scaleWebapplication,youunderstandthetermchange.Model-View-Controller(MVC)isadesignpatternputtogethertohelpcontrolchange.MVCdecouplesinterfacefrombusinesslogicanddata.StrutsisanMVCimplementationthatusesServlets2.2andJSP1.1tags,fromtheJ2EEspecifications,aspartoftheimplementation.YoumayneverimplementasystemwithStruts,butlookingatStrutsmaygiveyousomeideasonyourfutureServletsandJSPimplementations.

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.Figure1displaystheWebadaptationofMVC,alsocommonlyknownasMVCModel2orMVC2.

Figure1.MVCModel2

Struts,anMVC2implementation

Strutsisasetofcooperatingclasses,servlets,andJSPtagsthatmakeupareusableMVC2design.ThisdefinitionimpliesthatStrutsisaframework,ratherthanalibrary,butStrutsalsocontainsanextensivetaglibraryandutilityclassesthatworkindependentlyoftheframework.Figure2displaysanoverviewofStruts.

Figure2.Strutsview

Strutsoverview

lClientbrowser

AnHTTPrequestfromtheclientbrowsercreatesanevent.TheWebcontainerwillrespondwithanHTTPresponse.

lController

TheControllerreceivestherequestfromthebrowser,andmakesthedecisionwheretosendtherequest.WithStruts,theControllerisacommanddesignpatternimplementedasaservlet.Thestruts-config.xmlfileconfigurestheController.

lBusinesslogic

Thebusinesslogicupdatesthestateofthemodelandhelpscontroltheflowoftheapplication.WithStrutsthisisdonewithanActionclassasathinwrappertotheactualbusinesslogic.

lModelstate

Themodelrepresentsthestateoftheapplication.Thebusinessobjectsupdatetheapplicationstate.ActionFormbeanrepresentstheModelstateatasessionorrequestlevel,andnotatapersistentlevel.TheJSPfilereadsinformationfromtheActionFormbeanusingJSPtags.

lView

TheviewissimplyaJSPfile.Thereisnoflowlogic,nobusinesslogic,andnomodelinformation--justtags.TagsareoneofthethingsthatmakeStrutsuniquecomparedtootherframeworkslikeVelocity.

Strutsdetails

DisplayedinFigure3isastripped-downUMLdiagramoftheorg.apache.struts.actionpackage.Figure6showstheminimalrelationshipsamongActionServlet(Controller),ActionForm(FormState),andAction(ModelWrapper).

Figure3:

therelationshipbetweenActionServlet(Controller)、ActionForm(FormState)andAction(ModelWrapper)

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.Actionstartedasaninterfaceandchan

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2