1、Controller1 详解Spring MVC的控制器ControllerA、Controller接口org.springframework.web.servlet.mvc.Controllerpublic interface Controller控制器是MVC中的C的部分。应用程序的行为可以理解为服务接口,而控制器使用户可以访问所提供的服务。控制器解析用户输入,并将其转换为合理的模型数据,并通过视图(view)展示给用户。Controller是一个接口,它抽象了控制器的概念,这样可以实现不同类型的Controller。如:表单控制器,向导控制器,命令控制器等。Base Controller
2、 interface, representing a component that receives HttpServletRequest and HttpServletResponse instances just like a HttpServlet but is able to participate in an MVC workflow. Controllers are comparable to the notion of a Struts Action.基础Controller接口是一个组件,它接收HttpServletRequest和HttpServletResponse实例,就
3、像一个HttpServlet,但是它可以参与MVC工作流。它相当于Struct中的notion。WorkflowAfter a DispatcherServlet has received a request and has done its work to resolve locales, themes and suchlike, it then tries to resolve a Controller, using a HandlerMapping. When a Controller has been found to handle the request, the handleReq
4、uest method of the located Controller will be invoked; the located Controller is then responsible for handling the actual request and - if applicable - returning an appropriate ModelAndView. So actually, this method is the main entrypoint for the DispatcherServlet which delegates requests to control
5、lers. This method - and also this interface - should preferrably not be implemented by custom controllers directly, since abstract controller also provided by this package already provide a lot of functionality for typical use cases in web applications. A few examples of those controllers: AbstractC
6、ontroller, AbstractCommandController, SimpleFormController.流程在DispatcherServlet接收一个请求,并且完成了locale,theme等的解析工作,之后它将根据HandlerMapping来解析一个Controller。当这个Controller被发现,就使用它来处理这个请求,那么这个Controller的handleRequest方法将被调用。然后这个Controller将负责处理这个实际的请求,若可能的话它将返回一个对应的ModelAndView实例。实际上,这个方法(handleRequest)是Dispatcher
7、Servlet把requets委托到Controller的一个入口。这个方法最好不要由自定义的Controller直接来实现,因为这个包里的抽象controller已经提供许多功能,这些功能满足web应用的常见的用例。例如,AbstractController,AbstractCommandController,,SimpleFormController。So basically any direct implementation of the Controller interface just handles HttpServletRequests and should return a M
8、odelAndView, to be further interpreted by the DispatcherServlet. Any additional functionality such as optional validation, form handling, etc should be obtained through extending one of the abstract controller classes mentioned above. 任何直接实现Controller接口的实例,它只是处理HttpServletRequests并返回一个ModelAndView。任
9、何附加功能,如,可选验证、表单处理等,就需要通过扩展上面提及的抽象类来实现了。抽象方法ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) Process the request and return a ModelAndView object which the DispatcherServlet will render. 处理请求返回ModelAndView对象给DispatcherServlet处理。B、AbstractController抽象类org.springfram
10、ework.web.servlet.mvc.AbstractControllerpublic abstract class AbstractController extends WebContentGenerator implements Controllerjava.lang.Object org.springframework.context.support.ApplicationObjectSupport org.springframework.web.context.support.WebApplicationObjectSupport org.springframework.web.
11、servlet.support.WebContentGenerator org.springframework.web.servlet.mvc.AbstractControllerConvenient superclass for controller implementations, using the Template Method design pattern.Controller接口的抽象基础类,使用Template方法设计模式。Workflow (and that defined by interface):1、handleRequest() will be called by th
12、e DispatcherServlet2、Inspection of supported methods (ServletException if request method is not support)3、If session is required, try to get it (ServletException if not found)4、Set caching headers if needed according to cacheSeconds propery5、Call abstract method handleRequestInternal() (optionally s
13、ynchronizing around the call on the HttpSession), which should be implemented by extending classes to provide actual functionality to return ModelAndView objects.流程1、 DispatcherServlet调用handleRequest()方法。2、 检查支持的方法(若请求方法不支持抛出ServletException)。3、 若需要session,则获取session(若没找到则抛出ServletException)。4、 若需要c
14、acheSeconds属性,则设置缓存头。5、 调用抽象方法handleRequestInternal(可选,在HttpSession上同步调用),这个方法应被它的扩展类实现来提供实际的功能来返回一个ModelAndView的实例。Exposed configuration properties (and those defined by interface):namedefaultdescriptionsupportedMethodsGET,POSTcomma-separated (CSV) list of methods supported by this controller, such
15、 as GET, POST and PUT指定controller支持的方法。requireSessionfalsewhether a session should be required for requests to be able to be handled by this controller. This ensures that derived controller can - without fear of null pointers - call request.getSession() to retrieve a session. If no session can be fo
16、und while processing the request, a ServletException will be thrown指定request是否需要http session。cacheSeconds-1indicates the amount of seconds to include in the cache header for the response following on this request. 0 (zero) will include headers for no caching at all, -1 (the default) will not generateany headersand any positive number will generate headers that state the amount indi
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1