中英文翻译--Spring的web MVC 构架模式(精品doc)Word格式文档下载.docx
《中英文翻译--Spring的web MVC 构架模式(精品doc)Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《中英文翻译--Spring的web MVC 构架模式(精品doc)Word格式文档下载.docx(5页珍藏版)》请在冰豆网上搜索。
JuergenHoeller
1. Introduction:
SpringtheApplicationFramework
WhenfirstconfrontedwiththeSpringFramework,onemightbetemptedtothink:
〃0hno,notyetanotherwebframework^.ThisarticlewilloutlinewhySpringisn,tparticularlyawebframeworkbutagenericlightweightapplicationframeworkwithdedicatedwebsupport,andshowthearchitecturaldifferencestoStrutsandWebWorkIncontrasttoStrutsorWebWork,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,we11-architectedwebapplicationstorunina"
simple"
containerlikeTomcatorResin.
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'
sWebFramework
Spring,swebframeworkisdesignedaroundaDispatcherServletthatdispatchesrequeststohandlers,withconfigurablehandlermappings,viewresolution,andlocaleandthemeresolution.ThedefaulthandlerisaverysimpleControllerinterface,justofferinga/zModelAndViewhandleRequest(request,response)〃method.Thiscanalreadybeusedforapplicationcontrollers,butyouwillprefertheincludedimplementationhierarchy,consistingofAbstractcontroller,AbstractCommandControIler,MultiActionController,SimpleFormController,AbstractWizardFormControIler.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).Viewnam