1. DispatcherServlet is an implementation of the front controller design pattern that provides a centralized access point for Spring Web MVC, is responsible for the assignment of responsibilities, and integrates seamlessly with the Spring IoC container to get all the benefits of Spring.
DispatcherServlet is mainly used for responsibility scheduling, and itself is mainly used to control the process. The main responsibilities are as follows:
1. File upload parsing, if the request type is multipart, the file upload parsing will be performed through MultipartResolver;
2. Through HandlerMapping, map the request to the processor (return a HandlerExecutionChain, which includes a processor and multiple HandlerInterceptor interceptors);
3. Support multiple types of processors (processors in HandlerExecutionChain) through HandlerAdapter;
4. Parse the logical view name to the specific view through ViewResolver;
5. Localization analysis;
6. Rendering specific views, etc.;
<servlet> <servlet-name> hra</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> hra</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet>
The DispatcherServlet uses WebApplicationContext as the context by default, and the Spring default configuration file is "/WEB-INF/[servlet name]-servlet.xml"
2. Special beans used by DispatcherServlet:
DispatcherServlet uses WebApplicationContext as the context by default, so let's take a look at what special beans are in this context:
1. Controller : processor/page controller , which does the things of C in MVC, but the control logic is transferred to the front-end controller for processing requests;
2. HandlerMapping : The mapping from requests to processors, if the mapping is successful, a HandlerExecutionChain object (including a Handler processor (page controller) object, multiple HandlerInterceptor interceptors) objects is returned; for example, BeanNameUrlHandlerMapping maps URLs to Bean names, and the mapping is successful The Bean is the processor here;
3. HandlerAdapter : HandlerAdapter will package the processor as an adapter, so as to support multiple types of processors, that is, the application of the adapter design pattern, so it is easy to support many types of processors; for example, SimpleControllerHandlerAdapter will implement the Bean that implements the Controller interface. Adapt, and drop the handleRequest method of the processor for functional processing;
4. ViewResolver : ViewResolver will parse the logical view name into a specific View. Through this strategy mode, it is easy to replace other view technologies; for example, InternalResourceViewResolver maps logical view names to jsp views;
5. LocalResover : Localization analysis, because Spring supports internationalization, so LocalResover parses the client's Locale information to facilitate internationalization;
6. ThemeResovler : Theme analysis, through which multiple styles of a page can be realized, that is, common effects similar to software skins;
7. MultipartResolver : file upload analysis , used to support file upload;
8. HandlerExceptionResolver : Handler exception analysis , which can map exceptions to the corresponding unified error interface, so as to display a user-friendly interface (instead of showing the user specific error information);
9. RequestToViewNameTranslator : When the processor does not return the logical view name and other related information, the request URL is automatically mapped to the logical view name;
::: Set the Servlet initialization parameters (init-param) to the component (such as contextAttribute, contextClass, namespace, contextConfigLocation), simplify the setting process through BeanWrapper, and facilitate subsequent use;
Initialize the web context;
public class DispatcherServlet extends FrameworkServlet {
// Implement the onRefresh() method of the subclass, which is delegated to the initStrategies() method.
@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
//Initialize the strategy used by the default Spring Web MVC framework (such as HandlerMapping)
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);//File upload
initLocaleResolver(context);//Localization resolution
initThemeResolver(context);//Theme resolution
initHandlerMappings(context);//Map the request to the processor
initHandlerAdapters(context);//Adapter, corresponding to the processor
initHandlerExceptionResolvers(context);//Exception
initRequestToViewNameTranslator(context);
initViewResolvers(context);//View resolution
initFlashMapManager(context);
}
}
When the web container starts, the DispatcherServlet is initialized and it does the following:
DispatcherServlet is a foreground controller, configured in web.xml, to intercept matching requests. Servlet interception and matching rules must be defined by themselves, and the intercepted requests are distributed to the target Controller according to certain rules. This process requires HandlerMapping and HandlerAdapter.