Controller1 详解Spring MVC的控制器Controller.docx

上传人:b****5 文档编号:2780307 上传时间:2022-11-15 格式:DOCX 页数:19 大小:26.46KB
下载 相关 举报
Controller1 详解Spring MVC的控制器Controller.docx_第1页
第1页 / 共19页
Controller1 详解Spring MVC的控制器Controller.docx_第2页
第2页 / 共19页
Controller1 详解Spring MVC的控制器Controller.docx_第3页
第3页 / 共19页
Controller1 详解Spring MVC的控制器Controller.docx_第4页
第4页 / 共19页
Controller1 详解Spring MVC的控制器Controller.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

Controller1 详解Spring MVC的控制器Controller.docx

《Controller1 详解Spring MVC的控制器Controller.docx》由会员分享,可在线阅读,更多相关《Controller1 详解Spring MVC的控制器Controller.docx(19页珍藏版)》请在冰豆网上搜索。

Controller1 详解Spring MVC的控制器Controller.docx

Controller1详解SpringMVC的控制器Controller

A、Controller接口

org.springframework.web.servlet.mvc.Controller

publicinterfaceController

控制器是MVC中的C的部分。

应用程序的行为可以理解为服务接口,而控制器使用户可以访问所提供的服务。

控制器解析用户输入,并将其转换为合理的模型数据,并通过视图(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

org.springframework.web.servlet.mvc.AbstractController

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 党团建设

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1