2. Understanding DispatcherServlet

Source: Internet
Author: User

To understand DispatcherServlet, first let's take a look at its role. First, let's abstract the original words of Kaitao:

DispatcherServlet is the implementation of the front-end controller design pattern. It provides Spring Web MVC centralized access points and is responsible for assigning responsibilities. It is also seamlessly integrated with Spring IoC containers to achieve all the benefits of Spring.

What is the front-end controller design mode? The following is a text excerpt from Baidu:

The front-end controller provides a controller that can centrally manage requests. A front-end controller can accept all customer requests and send each request to the corresponding request handle, and respond to the user as appropriate.

The front-end controller is also the design mode of the presentation layer. It appears mainly because the presentation layer usually needs to control and coordinate multiple requests from different users, and this control mechanism depends on different needs, it may be centrally controlled or distributed. In other words, the application system needs to provide a centralized control module for presentation layer requests to provide various system services, including content extraction, view management, and browsing, if the system does not have such a centralized control module or control mechanism, each different system service needs to be processed in a separate view, so that code duplication will increase, resulting in a higher system development cost. At the same time, if there is no browsing mechanism between fixed module management views, so that its browsing function is delegated to different views, the maintainability of the system will be damaged.

Don't care about these details. You probably know that the purpose of this design model is to centralize customer requests and assign responsibilities. The point is that it is seamlessly integrated with the Spring IOC container. It is definitely seamless for the same company.

Now let's take a look at his role, from the original article of Kai Tao:

DispatcherServlet is mainly used for job scheduling and is mainly used to control the process. Its main responsibilities are as follows:

1. File Upload parsing. If the request type is multipart, the file upload will be parsed through MultipartResolver;

2. Map requests to the processor through HandlerMapping (a HandlerExecutionChain is returned, which includes one processor and multiple HandlerInterceptor interceptors );

3. HandlerAdapter supports multiple types of processors (processors in HandlerExecutionChain );

4. Use ViewResolver to parse the logical view name to a specific view;

5. Local parsing;

6. Rendering specific views;

7. If an exception occurs during execution, HandlerExceptionResolver will be handed over for parsing.

Please refer to the http://jinnianshilongnian.iteye.com/blog/1602617

Break down. Add more materials:

DispatcherServlet uses WebApplicationContext as the context by default, so let's take a look at the special beans in this context:

1, Controller:The processor/page controller does C in MVC, but the control logic is transferred to the front-end controller for processing requests;

2, HandlerMapping:Request-to-processor Ing. If the ING is successful, a HandlerExecutionChain object (including one Handler processor (page controller) object and multiple HandlerInterceptor interceptors) is returned. For example, BeanNameUrlHandlerMapping maps the URL and Bean name, the successfully mapped Bean is the processor here;

3, HandlerAdapter:HandlerAdapter will package the processor as an adapter to support a variety of types of processors, that is, the application of the adapter design mode, so that it can easily support many types of processors; for example, SimpleControllerHandlerAdapter will adapt the Bean implementing the Controller interface, and remove the handleRequest method of the processor for functional processing;

4, ViewResolver:ViewResolver resolves the logical View name to a specific View. This policy mode makes it easy to change other View Technologies. For example, InternalResourceViewResolver maps the logical View name to a jsp View;

5, LocalResover:Localization parsing. Because Spring supports internationalization, LocalResover parses the Locale information of the client to facilitate internationalization;

6ThemeResovler:Theme parsing, through which multiple styles of a page are implemented, that is, common skin effects similar to software;

7, MultipartResolver:File upload parsing, used to support file upload;

8,HandlerExceptionResolver:Processor exception parsing can map exceptions to the corresponding unified error interface to display a user-friendly interface (rather than showing users specific error information );

9,RequestToViewNameTranslator:When the processor does not return information such as the logical view name, the request URL is automatically mapped to the logical view name;

10,FlashMapManager:A policy interface used to manage FlashMap. FlashMap is used to store the output of a request. When a request enters another request, it is used as the input of the request. It is usually used for redirect scenarios and will be described later.




2. Understanding DispatcherServlet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.