您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Web MVC with the Spring Framework
1WebMVCwiththeSpringFrameworkJuergenHoeller1.Introduction:SpringtheApplicationFrameworkWhenfirstconfrontedwiththeSpringFramework,onemightbetemptedtothink:Ohno,notyetanotherwebframework.ThisarticlewilloutlinewhySpringisn'tparticularlyawebframeworkbutagenericlightweightapplicationframeworkwithdedicatedwebsupport,andshowthearchitecturaldifferencestoStrutsandWebWork.IncontrasttoStrutsorWebWork,Springisanapplicationframeworkforalllayers:Itoffersabeanconfigurationfoundation,AOPsupport,aJDBCabstractionframework,abstracttransactionsupport,etc.Itisaverynon-intrusiveeffort:YourapplicationclassesdonotneedtodependonanySpringclassesifnotnecessary,andyoucanreuseeverypartonitsownifyouliketo.Fromitsverydesign,theframeworkencouragescleanseparationoftiers,mostimportantlywebtierandbusinesslogic:e.g.thevalidationframeworkdoesnotdependonwebcontrollers.Majorgoalsarereusabilityandtestability:Unnecessarycontainerorframeworkdependenciescanbeconsideredavoidableevils.Ofcourse,Spring'sownwebsupportisnicelyintegratedwiththeframework'sgeneralpatterns.Nevertheless,replacingthewebsolutionwithStruts,WebWork,orthelikeiseasy.BothwithSpring'swebsupportoradifferentone,Springallowsforbuildingatruededicatedmiddletierinthewebcontainer,withtheoptiontoreuseexactlythesamebusinesslogicintestenvironmentsorstandaloneapplications.AndwithinJ2EE,yourbusinesslogicwillnotunnecessarilydependoncontainerserviceslikeJTAorEJB-allowingcomplex,well-architectedwebapplicationstoruninasimplecontainerlikeTomcatorResin.NotethatSpringdoesn'tgenerallyaimtocompetewithexistingsolutions.ItratherfostersseamlessintegrationwithstandardslikeServlet,JSP,JTA,JNDI,JDBC,andJDO,andwell-suitedtoolslikeHibernate,Velocity,Log4J,andCaucho'sHessian/Burlap.Theframeworkisdesignedtogrowwiththeneedsofyourapplications,intermsoftechnologychoice:Forexample,youwillprobablyuseJTAviaSpring'sJtaTransactionManagerifyouneeddistributedtransactions-butonlythen,asthereareperfectreplacementsforsingledatabases,likeDataSourceTransactionManagerorHibernateTransactionManager.2.WebMVC:TheDesignofSpring'sWebFramework2Spring'swebframeworkisdesignedaroundaDispatcherServletthatdispatchesrequeststohandlers,withconfigurablehandlermappings,viewresolution,andlocaleandthemeresolution.ThedefaulthandlerisaverysimpleControllerinterface,justofferingaModelAndViewhandleRequest(request,response)method.Thiscanalreadybeusedforapplicationcontrollers,butyouwillprefertheincludedimplementationhierarchy,consistingofAbstractController,AbstractCommandController,MultiActionController,SimpleFormController,AbstractWizardFormController.Applicationcontrollerswilltypicallybesubclassesofthose.Notethatyoucanchooseanappropriatebaseclass:Ifyoudon'thaveaform,youdon'tneedaFormController.ThisisamajordifferencetoStruts.Youcantakeanyobjectascommandorformobject:There'snoneedtoimplementaninterfaceorderivefromabaseclass.Spring'sdatabindingishighlyflexible,e.g.ittreatstypemismatchesasvalidationerrorsthatcanbeevaluatedbytheapplication,notassystemerrors.Soyoudon'tneedtoduplicateyourbusinessobjects'propertiesasStringsinyourformobjects,justtobeabletohandleinvalidsubmissions,ortoconverttheStringsproperly.Instead,it'softenpreferabletobinddirectlytoyourbusinessobjects.ThisisanothermajordifferencetoStrutswhichisbuiltaroundrequiredbaseclasseslikeActionandActionForm-foreverytypeofaction.ComparedtoWebWork,Springhasmoredifferentiatedobjectroles:ItsupportsthenotionofaController,anoptionalcommandorformobject,andamodelthatgetspassedtotheview.Themodelwillnormallyincludethecommandorformobjectbutalsoarbitraryreferencedata.Instead,aWebWorkActioncombinesallthoserolesintoonesingleobject.WebWorkdoesallowyoutouseexistingbusinessobjectsaspartofyourform,butjustbymakingthembeanpropertiesoftherespectiveActionclass.Finally,thesameActioninstancethathandlestherequestgetsusedforevaluationandformpopulationintheview.Thus,referencedataneedstobemodelledasbeanpropertiesoftheActiontoo.Thesearearguablytoomanyrolesinoneobject.Regardingviews:Spring'sviewresolutionisextremelyflexible.AControllerimplementationcanevenwriteaviewdirectlytotheresponse,returningnullasModelAndView.Inthenormalcase,aModelAndViewinstanceconsistsofaviewnameandamodelMap,containingbeannamesandcorrespondingobjects(likeacommandorform,referencedata,etc).Viewnameresolutionishighlyconfigurable,eitherviabeannames,viaa3propertiesfile,orviayourownViewResolverimplementation.TheabstractmodelMapallowsforcompleteabstractionoftheviewtechnology,withoutanyhassle:BeitJSP,Velocity,oranythingelse-everyrenderercanbeintegrateddirectly.ThemodelMapsimplygetstransformedintoanappropriateformat,likeJSPrequestattributesoraVelocitytemplatemodel.3.Integration:UsingaDifferentWebFrameworkwithSpringManyteamswilltrytoleveragetheirinvestmentsintermsofknow-howandtools,bothforexistingprojectsandfornewones.Concretely,therearenotonlyalargenumberofbooksandtoolsforStrutsbutalsoalotofdevelopersthathaveexperiencewithit.Thus,ifyoucanlivewithStruts'architecturalflaws,itcanstillbeaviablechoicefortheweblayer.ThesameappliestoWebWorkandotherwebframeworks.Ifyoudon'twant
本文标题:Web MVC with the Spring Framework
链接地址:https://www.777doc.com/doc-4215448 .html