Controller1 详解Spring MVC的控制器ControllerWord文档格式.docx
《Controller1 详解Spring MVC的控制器ControllerWord文档格式.docx》由会员分享,可在线阅读,更多相关《Controller1 详解Spring MVC的控制器ControllerWord文档格式.docx(19页珍藏版)》请在冰豆网上搜索。
控制器解析用户输入,并将其转换为合理的模型数据,并通过视图(view)展示给用户。
Controller是一个接口,它抽象了控制器的概念,这样可以实现不同类型的Controller。
如:
表单控制器,向导控制器,命令控制器等。
BaseControllerinterface,representingacomponentthatreceivesHttpServletRequestandHttpServletResponseinstancesjustlikeaHttpServletbutisabletoparticipateinanMVCworkflow.ControllersarecomparabletothenotionofaStrutsAction.
基础Controller接口是一个组件,它接收HttpServletRequest和HttpServletResponse实例,就像一个HttpServlet,但是它可以参与MVC工作流。
它相当于Struct中的notion。
Workflow
AfteraDispatcherServlethasreceivedarequestandhasdoneitsworktoresolvelocales,themesandsuchlike,itthentriestoresolveaController,usingaHandlerMapping.WhenaControllerhasbeenfoundtohandletherequest,thehandleRequestmethodofthelocatedControllerwillbeinvoked;
thelocatedControlleristhenresponsibleforhandlingtheactualrequestand-ifapplicable-returninganappropriateModelAndView.Soactually,thismethodisthemainentrypointfortheDispatcherServletwhichdelegatesrequeststocontrollers.Thismethod-andalsothisinterface-shouldpreferrablynotbeimplementedbycustomcontrollersdirectly,sinceabstractcontrolleralsoprovidedbythispackagealreadyprovidealotoffunctionalityfortypicalusecasesinwebapplications.Afewexamplesofthosecontrollers:
AbstractController,AbstractCommandController,SimpleFormController.
流程
在DispatcherServlet接收一个请求,并且完成了locale,theme等的解析工作,之后它将根据HandlerMapping来解析一个Controller。
当这个Controller被发现,就使用它来处理这个请求,那么这个Controller的handleRequest方法将被调用。
然后这个Controller将负责处理这个实际的请求,若可能的话它将返回一个对应的ModelAndView实例。
实际上,这个方法(handleRequest)是DispatcherServlet把requets委托到Controller的一个入口。
这个方法最好不要由自定义的Controller直接来实现,因为这个包里的抽象controller已经提供许多功能,这些功能满足web应用的常见的用例。
例如,AbstractController,AbstractCommandController,,SimpleFormController。
SobasicallyanydirectimplementationoftheControllerinterfacejusthandlesHttpServletRequestsandshouldreturnaModelAndView,tobefurtherinterpretedbytheDispatcherServlet.Anyadditionalfunctionalitysuchasoptionalvalidation,formhandling,etcshouldbeobtainedthroughextendingoneoftheabstractcontrollerclassesmentionedabove.
任何直接实现Controller接口的实例,它只是处理HttpServletRequests并返回一个ModelAndView。
任何附加功能,如,可选验证、表单处理等,就需要通过扩展上面提及的抽象类来实现了。
抽象方法
ModelAndViewhandleRequest(HttpServletRequestrequest,HttpServletResponseresponse)
ProcesstherequestandreturnaModelAndViewobjectwhichtheDispatcherServletwillrender.
处理请求返回ModelAndView对象给DispatcherServlet处理。
B、AbstractController抽象类
org.springframework.web.servlet.mvc.AbstractController
publicabstractclassAbstractControllerextendsWebContentGeneratorimplementsController
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.support.WebContentGenerator
Convenientsuperclassforcontrollerimplementations,usingtheTemplateMethoddesignpattern.
Controller接口的抽象基础类,使用Template方法设计模式。
Workflow(andthatdefinedbyinterface):
1、handleRequest()willbecalledbytheDispatcherServlet
2、Inspectionofsupportedmethods(ServletExceptionifrequestmethodisnotsupport)
3、Ifsessionisrequired,trytogetit(ServletExceptionifnotfound)
4、SetcachingheadersifneededaccordingtocacheSecondspropery
5、CallabstractmethodhandleRequestInternal()(optionallysynchronizingaroundthecallontheHttpSession),whichshouldbeimplementedbyextendingclassestoprovideactualfunctionalitytoreturnModelAndViewobjects.
1、DispatcherServlet调用handleRequest()方法。
2、检查支持的方法(若请求方法不支持抛出ServletException)。
3、若需要session,则获取session(若没找到则抛出ServletException)。
4、若需要cacheSeconds属性,则设置缓存头。
5、调用抽象方法handleRequestInternal(可选,在HttpSession上同步调用),这个方法应被它的扩展类实现来提供实际的功能来返回一个ModelAndView的实例。
Exposedconfigurationproperties(andthosedefinedbyinterface):
name
default
description
supportedMethods
GET,POST
comma-separated(CSV)listofmethodssupportedbythiscontroller,suchasGET,POSTandPUT
指定controller支持的方法。
requireSession
false
whetherasessionshouldberequiredforrequeststobeabletobehandledbythiscontroller.Thisensuresthatderivedcontrollercan-withoutfearofnullpointers-callrequest.getSession()toretrieveasession.Ifnosessioncanbefoundwhileprocessingtherequest,aServletExceptionwillbethrown
指定request是否需要httpsession。
cacheSeconds
-1
indicatestheamountofsecondstoincludeinthecacheheaderfortheresponsefollowingonthisrequest.0(zero)willincludeheadersfornocachingatall,-1(thedefault)willnotgenerate
anyheaders
andanypositivenumberwillgenerateheadersthatstatetheamountindi