Struts开源框架中英文对照外文翻译文献.docx
- 文档编号:27977097
- 上传时间:2023-07-07
- 格式:DOCX
- 页数:14
- 大小:29.21KB
Struts开源框架中英文对照外文翻译文献.docx
《Struts开源框架中英文对照外文翻译文献.docx》由会员分享,可在线阅读,更多相关《Struts开源框架中英文对照外文翻译文献.docx(14页珍藏版)》请在冰豆网上搜索。
Struts开源框架中英文对照外文翻译文献
中英文对照外文翻译文献
(文档含英文原文和中文翻译)
BestpracticesforStrutsdevelopment
Struts:
Abriefintroduction
Struts,anopensourceframeworkyoucanusetobuildWebapplications,isbasedonthepopularModel-View-Controller(MVC2)designparadigm.TheframeworkisbuiltuponstandardtechnologieslikeJavaServlets,JavaBeans,ResourceBundles,andXML,anditprovidesflexibleandextensiblecomponents.StrutsimplementstheControllerlayerintheformof ActionServlet andrecommendsbuildingtheViewlayerusingJSPtaglibraries.StrutsalsoprovidesawrapperaroundtheModellayerthroughAction classes.Figure1illustratestheStrutsframeworkbasedontheModel-View-Controllerdesign.
Figure1.StrutsandMVC
OverviewofStrutscomponents
First,we'llexplaintheStrutscomponentsinthecontextofbestpracticesandtheroleeachoneplaysinyourWebapplicationdevelopment.
Action
Every Action ofyourapplicationextendsStruts' org.apache.struts.action.Action.These Action classesprovideaninterfacetotheapplication'sModellayer,actingasawrapperaroundthebusinesslogic.Each Action classmustprovideitscase-specificimplementationtothe perform() method.The perform() methodalwaysreturnsavalueoftype ActionForward.
ActionForm
Every ActionForm ofyourapplicationextendsStruts' org.apache.struts.action.ActionForm. ActionFormsaresimpleJavaBeansthatencapsulateandvalidaterequestparameters.Tovalidateyourrequestdata,your ActionForm's validate() methodmustgiveacase-specificimplementation. ActionFormsserveasacarrierofrequestdatatothe Action class.AJSPobjectcombineswitharespective ActionForm toformyourapplication'sViewlayer,wherealmosteveryformfieldoftheJSPobjectmapstoanattributeofthecorresponding ActionForm.
JSPcustomtaglibraries
TheJSPcustomtaglibrariesareacollectionofactionspresentedastags.ThisisapowerfulfeatureoftheJSPSpecification1.1;itallowsyoutoseparatepresentationfromotherapplicationtiers.ThelibrariesareeasytouseandyoucanreadtheminXML-likefashion.YoucaneasilymaintaintheJSPcomponentsbyminimizingtheuseofJavascriptletsinthem.TheJSPtagsthatStrutsprovidesincludeHTML,logic,andbeantags.
ActionErrors
Youuse ActionErrorstosupportexceptionhandling.An ActionError trapsandpropagatesanapplicationexceptiontotheViewlayer.Eachoneisacollectionof ActionError instances. ActionErrorsencapsulateerrormessages,whilethe
errors> inthePresentationlayerrendersallerrormessagesinthe ActionError collection.
BestPractice1.ReusedataacrossmultipleActionForms
NowthatyouarefamiliarwiththeStrutscomponents,wewillcontinuebyshowingyouwaystogetthemostoutoftheframework.First,StrutsrecommendsthatyouassociateeveryJSPobjectwithan ActionForm,whichencapsulatesdatarepresentedinthescreen.YouaccesstheformdataintheJSPobjectusingaccessorymethodsfoundin ActionForm.Listing1showstheconventionaluseof ActionForm tagintheViewlayer.
Listing1.UsingActionForminJSP
formaction="/bp1"> textproperty="attrib1"/> form> The ActionForm called"BP1AForm"includestheattribute attrib1,aswellasitsgetterandsettermethods.Intheconfigurationfilestruts-config.xml,theaction"/bp1"mapsto bp1AForm usingthe name attribute.ThisfacilitatesdatadisplayintheJSP. Toimplementthisbestpractice,Strutsrecommendsyoudotwothings: 1.CreateaJavaBean(BP1BForm)withattributesthatformanattributesubsetin BP1AForm,alongwiththeattributes'getterandsettermethods. 2.Replacetheattributesin BP1AForm withthebean BP1BForm byassociatingthebeanwith BP1AForm.Nowyoucanaccessthisattributesubsetin BP1AForm through BP1BForm.Listing2showsyouhow. Listing2.AccessingformattributesinJSP formaction="/bp1"> definename="bp1AForm"property="bp1BForm"id="bp1B" type="com.ibm.dw.webarch.struts.BP1BForm"/> textname="bp1B"property="subsetAtt1"/> form> BestPractice2.UseActionclasstohandlerequests TypicallywhenusingtheStrutsframework,foreveryactiontheJSPcomponentrequestsyourapplicationtoexecute,theapplicationmustextendStruts' org.apache.struts.action.Action tocreatean Action class.Thisindividual Action classinterfaceswiththeapplication'sModellayerwhileprocessingtherequest. Toimplementthispractice,Strutsrecommendsyoufollowthesesteps: 1.Createan Action class,say BP2Action,byextending org.apache.struts.action.Action. 2.Createallother Action classesinyourWebapplicationbyextending BP2Action. 3.In BP2Action,createamethod performTask(),asinpublicabstract ActionForwardperformTask(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException. 4.In BP2Action addoneormoregenericmethodstotheapplication,forexample serverSideValidate().Youcandecideonthemethod'saccessmodifierbyconsideringthefollowingfactors: oIfall Action classesmustimplementthismethod,makeitabstract. oIfsome Action classeswillprovideacase-specificimplementation,declarethemethodprotectedandgiveitadefaultimplementation. 5.In BP2Action,declaremethod perform() asfinal.Invoketheabovegenericmethod,whichmustalwaysbecalledbeforeprocessingtherequest.Nowcallthemethod performTask() createdin step3. 6.Inevery Action classextending BP2Action,addmethod performTask() withacase-specificimplementation. Advantages Thispracticehastwomainadvantages.First,ithelpsyouavoidredundantcodeinevery Action classofyourWebapplication.Second,itgivestheapplicationmorecontrolovergenerictasksbycentralizingthebehaviorinone Action class. BestPractice3.UseActionFormtoworkonsessiondata InaStruts-basedWebapplication,each ActionForm extends org.apache.struts.action.ActionForm.These ActionFormsencapsulatepagedataandprovideavalidationframeworktovalidaterequestparameters. MostWebapplicationsmaintaindatainsessiontomakethemavailablethroughouttheapplication.ThisbestpracticeaddressesthisWebapplicationfeature.Itallowsmethods toSession() and fromSession() tomovesessiondatatoandfromtheformdata.Thus,itaddressessessiondatamaintenanceinaWebapplication. Toadheretothispractice,followthesesteps: 1.Createanabstractclassnamed BP3Form byextending org.apache.struts.action.ActionForm. 2.In BP3Form,addmethodswithaccessmodifiersasinpublicabstract voidtoSession(SessionDatasessionData) and voidfromSession(SessionDatasessionData). 3.Inevery ActionForm,extend BP3Form andimplementtheabstractmethodsinwhichtheformdataistransportedtoandfromthesession. 4.Thecorresponding Action classmaydeterminetheorderinwhichthesemethodsarecalled.Forexample,youcouldinvokemethod toSession() onthe ActionForm justbefore actionForward isdetermined. Whentousethispractice Thispracticeismostusefulwhensessiondataismaintainedasasingleobjectand/oreverypagemanipulatesorusessessiondata. BestPractice4.Handleexceptionseffectively Conventionally,whenanapplicationexceptionoccursinan Action class,theexceptionisfirstlogged.ThentheclasscreatesanActionError andstoresitintheappropriatescope.This Action classthenforwardscontroltotheappropriate ActionForward.Listing3showshow Action classhandlesexceptions. Listing3.ExceptionhandlinginanActionclass try{ //CodeinActionclass } catch(ApplicationExceptione){ //logexception ActionErrorsactionErrors=newActionErrors(); ActionErroractionError=newActionError(e.getErrorCode()); actionErrors.add(ActionErrors.GLOBAL_ERROR,actionError); saveErrors(request,actionErrors); } Whileconventionalexceptionhandlingproceduressaveexceptioninformationinevery Action class,bestpractice4aimstoavoidredundantcodewhilehandlingexceptions. Tousethispractice,Strutsrecommendsfollowingthesesteps: 1.Createan Action class,say BP4Action,byextending org.apache.struts.action.Action. 2.Createallother Action classesinyourWebapplicationbyextending BP4Action. 3.In BP4Action,declarevariable ActionErrorsactionErrors=newActionErrors();. 4.In BP4Action,createamethod performTask() asinpublicabstract ActionForwardperformTask(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse,ActionErrorsactionErrors)throwsIOException,ServletException. 5.In BP4Action,declaremethod perform() asfinal.Theninvokegenericmethods,whichmustalwaysbecalledbeforeprocessingtherequest.Nowyoucancallthemethod performTask() createdinthepreviousstep. 6.Whileimplementingmethod performTask() inevery Action class(byextending BP4Action),handleapplicationexceptionsasshowninListing4. Listing4.UsingActionErrorseffectively try{ //CodeinActionclass } catch(ApplicationExceptionappException){ //Logexception //AdderrortoactionErrors actionErrors.add(ActionErrors.GLOBAL_ERROR, newActionError(appException.getErrorCode())); } In BP4Action,afterinvokingthemethod performTask(),savethe ActionErrors using saveErrors(request,errors). Advantages Thispractice'smainadvantageisthatitavoidscoderedundancyinevery Action classthathandles ActionErrors. Inconclusion BuildinganeasilymaintainableWebapplicationcanbeoneofthemostchallengingtasksforadevelopmentteam.UsingamatureframeworklikeStrutshelpsyouimplementtheinfrastructurecodenormallyassociatedwithbuildinganapplication.TheStrutsframeworkprovidesaset
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Struts 框架 中英文 对照 外文 翻译 文献