问题描述
原文:1.Introduction"Readthedirectionsanddirectlyyouwillbedirectedintherightdirection."TheframeworkdocumentationiswrittenforactivewebdevelopersandassumesaworkingknowledgeabouthowJavawebapplicationsarebuilt.Formoreabouttheunderlyingnutsandbolts,seetheKeyTechnologiesPrimer.1.1ForwardintothePast!(orabriefhistoryofStruts)WhenJavaservletswerefirstinvented,manyprogrammersquicklyrealizedthattheywereaGoodThing.TheywerefasterandmorepowerfulthatstandardCGI,portable,andinfinitelyextensible.ButwritingHTMLtosendtothebrowserinendlessprintln()statementswastiresomeandproblematic.TheanswertothatwasJavaServerPages,whichturnedServletwritinginside-out.NowdeveloperscouldeasilymixHTMLwithJavacode,andhavealltheadvantagesofservlets.Theskywasthelimit!Javawebapplicationsquicklybecame"JSP-centric".Thisin-and-ofitselfwasnotaBadThing,butitdidlittletoresolveflowcontrolissuesandotherproblemsendemictowebapplications.Clearly,anotherparadigmwasneeded...ManycleverdevelopersrealizedthatJavaServerPagesANDservletscouldbeusedtogethertodeploywebapplications.Theservletscouldhelpwiththecontrol-flow,andtheJSPscouldfocusonthenastybusinessofwritingHTML.Induecourse,usingJSPsandservletstogetherbecameknownasModel2(meaning,presumably,thatusingJSPsalonewasModel1).Ofcourse,thereisnothingnewundertheSun...andmanyhavebeenquicktopointoutthatJSP'sModel2followstheclassicModel-View-ControllerdesignpatternabstractedfromthevenerableSmalltalkMVCframework.JavaWebdevelopersnowtendtousethetermsModel2andMVCinterchangeably.Inthisguide,weusetheMVCparadigmtodescribetheframeworkarchitecture,whichmightbebesttermedaModel2/MVCdesign.TheApacheStrutsProjectwaslaunchedinMay2000byCraigR.McClanahantoprovideastandardMVCframeworktotheJavacommunity.InJuly2001,version1.0wasreleased,andIOHO,JavaModel2developmenthasneverbeenquitethesame.1.2TheModel-View-Controller('MVC')DesignPatternTheterm"MVC"originatedwiththeSmallTalkModel-View-Controllerframework.UnderMVC,anapplicationisseenashavingthreedistinctparts.TheproblemdomainisrepresentedbytheModel.TheoutputtotheuserisrepresentedbytheView.And,theinputfromtheuserisrepresentedbyController.1.2.1TheModel:SystemStateandBusinessLogicJavaBeansTheModelportionofanMVC-basedsystemcanbeoftenbedividedintotwomajorsubsystems--theinternalstateofthesystemandtheactionsthatcanbetakentochangethatstate.Ingrammaticalterms,wemightthinkaboutstateinformationasnouns(things)andactionsasverbs(changestothestateofthosethings).ManyapplicationsrepresenttheinternalstateofthesystemasasetofoneormoreJavaBeans.Thebeanpropertiesrepresentthedetailsofthesystem'state.Dependingonyourapplication'scomplexity,thesebeansmaybeselfcontained(andknowhowtopersisttheirownstate),ortheymaybefacadesthatknowhowtoretrievethesystem'sstatefromanothercomponent.Thiscomponentmaybeadatabase,asearchengine,anEntityEnterpriseJavaBean,aLDAPserver,orsomethingelseentirely.Large-scaleapplicationswilloftenrepresentthesetofpossiblebusinessoperationsasmethodsthatcanbecalledonthebeanorbeansmaintainingthestateinformation.Forexample,youmighthaveashoppingcartbean,storedinsessionscopeforeachcurrentuser,withpropertiesthatrepresentthecurrentsetofitemsthattheuserhasdecidedtopurchase.ThisbeanmightalsohaveacheckOut()methodthatauthorizestheuser'screditcardandsendstheordertothewarehousetobepickedandshipped.Othersystemswillrepresenttheavailableoperationsseparately,perhapsasSessionEnterpriseJavaBeans(SessionEJBs).Inasmallerscaleapplication,ontheotherhand,theavailableoperationsmightbeembeddedwithintheActionclassesthatarepartoftheframeworkcontrollayer.Thiscanbeusefulwhenthelogicisverysimpleorwherereuseofthebusinesslogicinotherenvironmentsisnotcontemplated.TheframeworkarchitectureisflexibleenoughtosupportmostanyapproachtoaccessingtheModel,butwestronglyrecommendthatyouseparatethebusinesslogic("howit'sdone")fromtherolethatActionclassesplay("whattodo").'nuffsaid.Formoreaboutadaptingyourapplication'sModeltotheframework,seetheBuildingModelComponentschapter.1.2.2TheView:JSPPagesandPresentationComponentsTheViewportionofaStruts-basedapplicationismostoftenconstructedusingJavaServerPages(JSP)technology.JSPpagescancontainstaticHTML(orXML)textcalled"templatetext",plustheabilitytoinsertdynamiccontentbasedontheinterpretation(atpagerequesttime)ofspecialactiontags.TheJSPenvironmentincludesasetofstandardactiontags,suchas<jsp:useBean>whosepurposeisdescribedintheJavaServerPagesSpecification.Inadditiontothebuilt-inactions,thereisastandardfacilitytodefineyourowntags,whichareorganizedinto"customtaglibraries."TheframeworkincludesasetofcustomtaglibrariesthatfacilitatecreatinguserinterfacesthatarefullyinternationalizedandinteractgracefullywithActionFormbeans.ActionFormscaptureandvalidatewhateverinputisrequiredbytheapplication.FormoreabouttheStrutstaglibsandusingpresentationpageswiththeframework,seetheBuildingViewComponentssection.AdditionaldocumentationregardingthetaglibsisalsoavailableintheTaglibssubproject.1.2.3TheController:ActionServletandActionMappingStrutsprovidestheControllerportionoftheapplication.TheControllerisfocusedonreceivingrequestsfromtheclient(typicallyauserrunningawebbrowser),decidingwhatbusinesslogicfunctionistobeperformed,andthendelegatingresponsibilityforproducingthenextphaseoftheuserinterfacetoanappropriateViewcomponent.TheprimarycomponentoftheControllerintheframeworkisaservletofclassActionServlet.ThisservletisconfiguredbydefiningasetofActionMappings.AnActionMappingdefinesapaththatismatchedagainsttherequestURIoftheincomingrequestandusuallyspecifiesthefullyqualifiedclassnameofanActionclass.AllActionsaresubclassedfrom[org.apache.struts.action.Action].Actionsencapsulatecallstobusinesslogicclasses,interprettheoutcome,andultimatelydispatchcontroltotheappropriateViewcomponenttocreatetheresponse.WhiletheframeworkdispatchestoaView,actuallyrenderingtheViewisoutsideitsscope.译文:1.导言“按照说明书,你就可以直接获得正确的方向定位”构架文档是为现行的web开发者和呈现javaweb应用程序是怎么样创建的应用知识而写的。想了解更多的具体细节,请参看KeyTechnologiesPrimer.1.1Struts简要历史当Javaservlets第一次被发明时,许多程序师很快就认识到Javaservlets是个好东西。它们由于使用标准的CGI,轻便,且可以无限扩展而运行速度快和功能强大。而在无止境的println()声明中写HTML来发送到浏览器是烦人的和问题百出的。答案是JavajServerPages彻底地写在Servlet中。现在开发者可以容易地将HTML和Java代码混合在一起,并拥用servlets的所有优势。天空是多少的有限!Javaweb应用程序迅速地成为“JSP-核心”.这并不是一件坏事,但它很少处理流控制问题和web应用程序的另一些特有问题。明显地,需要一些范例…很多聪明的开发者都意识到JavaServerPages和servlets可以结合使用来显示web应用程序。Servlets有助于流控制,而JSPS则将焦点放在令人厌恶的HTML业务书写上。众所周知,Model2中适当地将JSPS和servlets结合使用。(意思是推测起来,在Model1中单独使用JSPS)当然,Sun并没有添加新的东西…JSPSModel2遵循典型的古老的MVC框别架的MVC设计模型的很多特点很快被指出来。JavaWeb开发者试图使用Model2术语和可替交的MVC。在这种引导下,我们使用MVC范例去描述体系机构的框架是最好的Model2/MVC设计条件。CraigR.McClanahan在2000年5月发起ApacheStruts项目,并为Java社区提供标准的MVC框架。2001年7月发布1.0版本,IOHO,JavaModel2的发展不尽一样。1.2模型-视图-控制器设计模型“MVC“是SmallTalk模型-视图-控制器框架的缩略术语。在MVC下,明显地看出一个程序有三个部分。问题域由模型控制器来描述。输出给用户由视图控制器来描述。而用户输入则由控制器来描述。1.2.1模型:系统状态和JavaBeans业务逻辑基于MVC系统的Model部分通常可以分为两个主要的子系统—内部状态系统和用于改变状态的actions动作。在合乎文法的协商中,我们可以想象状态信息就像事件和动作(改变事件的状态)许多应用程序表现为类似于一系列JavaBeans的内部系统状态。Bean特性表现为系统详细资料的状态。决定于应用程序的复杂性,这些beans或许自包含(且知道怎么样维持他们自己的状态)且它们可以知道怎么样从另一个组件中重新获取系统状态。这些组件可能是一个数据库库,一个搜索引擎,一个企业级实体JavaBean,一个LDAP服务器,或是其它东西。大规模的应用程序通常将表现为一系列可能性的方法业务,它为bean和beans维持信息状态将义不容辞。例如,你或许有一个购物车bean,在session作用范围中为每一个当前用户存储购物车bean,它的特性表现为当前用户决定购买的一系列条目清单。这些bean总有一个checkOut()方法,这方法批准用户信用卡和发送命令到仓库去取货和载运。另一些系统则表现为有用的分离操作,或许像Session企业级JavaBeans.在小规模的应用程序中,另一方面,有用的操作可以作为框架中控制层的一部分而植入Action类中。当逻辑很简单或在另一些不可预期的环境中重新使用业务逻辑的地方变得非常有用。框架体系机构能弹性地支持大多数类似于模型的访问,但是我们强烈地推荐将业务逻辑(怎么做)从Action类的角色中分离出来。Nuff说。了解更多关于让你的应用程序模型配合框架,请参看BuildingModelComponents章节视图:JSP页和组件介绍1.2.2TheView:JSPPagesandPresentationComponents基于Struts的视图部分应用程序大部分是用JSP技术来构建的。JSP页能包含被称为模板的静态HTML(或XML),加上基于专用的解释(在页面请求时)型动作标签的插入动态内容的能力。JSP环境包含一系列标准的动作标签,例如<jsp:useBean>目的是描述JSP规范。除了内置的actions,有个标准的工具用于定义你的标签,这些存放在“自定义标签库”中。框架包含一系列自定义标签库,它使完全国际化的和ActionFormbeans相结合的用户界面更容易产生。ActionForms随时捕获并确认应用程序的输入请求。想了解更多Strutstaglibs和使用介绍框架事件,请参看BuildingViewComponents部分。附加的文档taglibs通常在子项目的Taglibs也可用到1.2.3控制器:ActionServlet和ActionMappingStruts充当应用程序的控制器部分。控制器的焦点集中在获取从客户端发送来的请求(典型地一个用户运行web浏览器),决定完成什么样的业务逻辑功能,一个适当的视图组件代表责任是产生下一个用户状态接口。框架中的控制器的主要组件是一个servlet类ActionServlet.这些servlet被定义一系列ActionMapping来配置。一个ActionMapping定义一个路径,输入请求的URL和通常的Action类的特性相匹配.所有的Actions都是[org.apache.struts.action.Action]的子类。Actions调用业务逻辑类,解释结果,最后分派控制适当的视图组件来产生响应。当框架派遣一个视图,实际上这个视图表现出超出了它的范围。----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------我现在正在翻译Struts1,请大家多帮忙!请大家帮我指正我的译文错误