DispatcherServlet introduction
DispatcherServlet is the implementation of the Spring front-end controller. It provides Spring Web MVC centralized access points and is responsible for assigning responsibilities. It is seamlessly integrated with Spring IoC containers to achieve all the benefits of Spring.
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.
DispatcherServlet uses WebApplicationContext as the context by default, and Spring's default configuration file is "/WEB-INF/[servlet name]-servlet. xml ".
DispatcherServlet can also configure its own initialization parameters to overwrite the default configuration:
|
Parameters
|
Description
|
|
ContextClass
|
Class that implements the WebApplicationContext interface. The current servlet uses it to create the context. If this parameter is not specified, XmlWebApplicationContext is used by default.
|
|
ContextConfigLocation
|
String passed to the context instance (specified by contextClass) to specify the context location. This string can be divided into multiple strings (using commas as the separator) to support multiple contexts (in the case of multiple contexts, if the same bean is defined twice, followed by a priority ).
Default:/WEB-INF/[server-name]-servlet. xml
|
|
Namespace
|
WebApplicationContext namespace. The default value is [server-name]-servlet.
|
As follows:
| The code is as follows: |
Copy code |
<Servlet>
<Servlet-name> demo </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value> classpath: spring-servlet-config.xml </param-value>
</Init-param>
</Servlet>
<Servlet-mapping>
<Servlet-name> demo </servlet-name>
<Url-pattern>/</url-pattern>
</Servlet-mapping>
|
Servlet context
The DispatcherServlet context is loaded by configuring the servlet contextConfigLocation. The default implementation is XmlWebApplicationContext.
It is worth noting that the DispatcherServlet context is only the context of Spring MVC, while the Spring loading context is loaded through ContextLoaderListener. Generally, spring web projects use both contexts. The former is only responsible for configuration management of MVC-related beans (such as ViewResolver, Controller, and MultipartResolver ), the latter is responsible for the configuration management (such as related services and DAO) of spring-related beans ).
Therefore, the Bean configured in/WEB-INF/[server-name]-servlet. xml is generally valid only for Spring MVC, while the bean configured in the ContextLoaderListener configuration file is valid for the whole spring.
After the context is created, it will be placed in the ServletContext object, where the context loaded by the ContextLoaderListener is placed in the key of the ServletContext as WebApplicationContext. in the ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE attribute, the context loaded by DispatcherServlet is put in the request object's key: WEB_APPLICATION_CONTEXT_ATTRIBUTE at each request. Therefore, the two methods are different. The former can be obtained through WebApplicationContextUtils. getRequiredWebApplicationContext (servletContext) or WebApplicationContextUtils. getWebApplicationContext (servletContext) or WebApplicationContextUtils. getWebApplicationContext (servletContext, attrname) method to obtain the corresponding applicationContext, while the latter uses RequestContextUtils. getWebApplicationContext (request) or WebApplicationContextUtils. getWebApplicationContext (servletContext, attrna To obtain the corresponding applicationContext.
Note: For the context loaded by ContextLoaderListener, attrname is the WebApplicationContext mentioned above. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; for DispatcherServlet, the context is FrameworkServlet. class. getName () + ". CONTEXT. "+ getServletName ()
According to the attributes of the context, if WebApplicationContextUtils. getRequiredWebApplicationContext (servletContext) is used to obtain the applicationContext loaded by DispatcherServlet, "No WebApplicationContext found: no ContextLoaderListener registered? "Exception.
Special Bean used in DispatcherServlet
DispatcherServlet uses WebApplicationContext as the context by default, so let's take a look at the special beans in this context:
1. Controller: processor/page Controller. It does the C in MVC, but the control logic is transferred to the front-end Controller to process the request;
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) object is returned; for example, BeanNameUrlHandlerMapping maps the URL to the Bean name. The Bean mapped successfully is the processor here;
3. HandlerAdapter: HandlerAdapter will package the processor as an adapter to support various types of processors, that is, applications in the adapter design mode, so as to 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. In this policy mode, it is easy to change other View Technologies. For example, InternalResourceViewResolver maps the logical View name to a jsp View;
5. LocalResover: localized resolution. Because Spring supports internationalization, LocalResover parses the Locale information of the client to facilitate internationalization;
6. ThemeResovler: ThemeResovler parses themes to implement multiple styles on a page, that is, common skin effects similar to software;
7. MultipartResolver: File Upload parsing, used to support file upload;
8. HandlerExceptionResolver: processor exception parsing. You can map exceptions to the corresponding unified error interface to display user-friendly interfaces (instead of 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 another request is entered, it is used as the input of the request. It is usually used for redirection scenarios.
From the bean above, we can see that generally LocalResover, ViewResolver and so on need to be configured in/WEB-INF/[server-name]-servlet. xml file.