Home > Back-end >  In the Spring MVC DispatcherServlet explanation
In the Spring MVC DispatcherServlet explanation

Time:11-26

introduction
DispatcherServlet is the realization of the front controller design pattern, provides SpringWebMVC centralized access point, and in charge of the duties assigned, and with the spring IOC container seamless integration, and thus can obtain the advantage of the spring,

responsibilities and role of
Its main responsibility is to dispatch work, used to control the process itself, specific as follows:

1. The file upload, if the request type is multipart file upload parsing through the MultipartResolver;
2. Through the HandlerMapping, mapping requests to processor (returns a HandlerExecutionChain, it includes a processor, multiple HandlerInterceptor interceptor);
3. Through handlerAdapter support multiple types of CPU (processor) in handlerExceptionChain;
4. Through ViewResolver parsing logic view name to view specific implementation;
5. The localization resolution;
6. Apply colours to a drawing of the specific views, etc.;
7. If abnormal encountered in the implementation process will be handed over to handlerExceptionResolver to parse;

DispatcherServler in the web. In XML configuration
 


Heiheihei


Org. Springframework. Web. Servlet. DispatcherServlet

1





Heiheihei

/& lt;/url - pattern>




DispatcherServlet initialization order
Inheritance system: HttpServlet<- HttpServletBean<- FrameworkServlet<- DispatcherServlet;

1. HttpServletBean inheritance HttpServlet, so in the web container startup will call its init () method, the main role of the initialization method is
(1) the servlet initialization parameters (init param) is set on the component (such as contextAttribute/contextClass/namespace/contextConfigLocation), through the BeanWrapper set value process simplified, convenient and subsequent use;
Extension point (2) provide a subclass is initialized, initServletBean (), the method by FrameworkServlet coverage,

2. FrameworkServlet HttpServletBean inheritance, through initServletBean () web context initialization, the method is mainly covers two things:
1) initialize the web context;
(2) provide a subclass initialization extension points,

3. The DispatcherServlet FrameworkServlet inheritance, and implement onRefresh () method provides some front controller related configuration; Main function:
1) initialize the spring web MVC use web context, and may specify the parent container;
(2) to initialize the dispatcherServlet use strategy, such as handlerMapping and handlerAdapter;



DispatcherServlet used in special bean

DispatcherServlet default WebApplicationContext as context, which contains a lot of beans:

Controller: processor/page controller, do the MVC in C, but the control logic is transferred to the front controller, used to deal with the request;

2. HandlerMapping: request the processor's mapping, if successfully returns a HandlerExecutionChain object mapping (contain a handler processor object, more handlerInterceptor interceptor); Such as BeanNameUrlHandlerMapping url and bean name mapping, mapping to the success of the bean is the processor;

3. HandlerAdapter: handlerAdapter will wrap the processor for the adapter, to support multiple types of processors, namely the application of the adapter design pattern, making it easy to support many types of processor; Such as SimpleControllerHandlerAdapter will Bean of implemented Controller interface adapter, and the processor is called handlerRequest method of function;

4. ViewResolver: ViewResolver the logical view to resolved to a specific view, through this strategy pattern, it is easy to replace the other tried to technology, such as InternalResourceViewResolver will logic view name mapped to the JSP view;

5. LocalResover: localization analysis, because Spring supports internationalization, therefore LocalResover parsing the client Locale information in order to convenient for internationalization;

6. ThemeResolver: theme, through it to practice county more than a page style, namely common skin effect is similar to the software;

7. MultipartResolver: file upload parsing, used to support file uploads;

8. HandlerExceptionResolver; Processor exception resolution, if the exception is mapped to the corresponding error unified interface, to display a user-friendly interface;

9. RequestToViewNameTranslator: when the processor does not return relevant information, such as logical view name automatically request url mapping for logical view name;

10. FlashMapManager: strategies for managing FlashMap interface, FlashMap used to store the output of a request, when to enter another request as input of the request, is often used to redirect scenario,

CodePudding user response:

Write it again to understand better, for SpringMVC principle of handwritten:
https://blog.csdn.net/songzehao/article/details/86759674
Lot of handwritten Spring MVC framework source address:
https://github.com/oaHeZgnoS/repo2019

CodePudding user response:

The building Lord speak well
  • Related