Dispatcher Servlet simple to understand and project configuration

This article focuses on the configuration and basic understanding of the Dispatcher Servlet in Spring in a project


A list,

The DispatcherServlet is an implementation of the front-end controller design pattern, provides a centralized point of access to Spring Web MVC, and is responsible for assigning responsibilities, seamlessly integrating with the Spring IoC container to reap all the benefits of Spring.

The Servlet intercepts matching requests in the web. XML file. The Servlet intercepts matching rules by itself and distributes intercepted requests to the target Controller for processing according to the corresponding rules.

Second, the role of

DispatcherServlet is mainly used for responsibility scheduling and itself is mainly used for control process. The main responsibilities are as follows:

If the request type is multipart, file upload resolver will be used for file upload resolver. 2. Map requests to handlers by HandlerMapping (return a HandlerExecutionChain containing one handler and multiple HandlerInterceptor interceptors); 3. Support multiple types of handlers through HandlerAdapter (handlers in HandlerExecutionChain); 4. Use ViewResolver to resolve the logical view name to the concrete view implementation; 5. Localization analysis; 6. Render specific views, etc. 7. If an exception is encountered during execution, HandlerExceptionResolver will resolve it.

Web. XML configuration

Web.xml file configuration:

<! <context-param> <param-name>contextConfigLocation</param-name> <param-value> Classpath *:/applicationContext. XML </param-value> </context-param> <listener> <description>Spring listener </description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <! -- Spring MVC --> <servlet> <! <servlet-name> Dispatcher Servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <! <load-on-startup>1</load-on-startup> <! If init-param is not configured, the DispatcherServlet uses WebApplicationContext by default. The default configuration file for Spring is "/ web-INF /[servlet name]-servlet.xml" --> <! --> <init-param> <param-name>contextConfigLocation</param-name> <! <param-value>classpath*:/spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <! Intercepting matching requests, DispatcherServlet </servlet-name> <url-pattern>*.htm</url-pattern> <url-pattern>*.do</url-pattern> <url-pattern>*.json</url-pattern> <! - configuration "/" intercept all - > < url - the pattern > / < / url - the pattern > < / servlet mapping - >Copy the code

Spring-mvc.xml file configuration:

<! Automatically register the Bean using the Annotation, just scan @Controller --> <! -- Base-package If there are multiple base-packages, separate them with commas (,) --> <context: Component-scan base-package="com.xxx.web.**.*,com.xxx.common.web.**.*" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/ > <! -- Spring3.2 new annotation @controllerAdvice, using the @Component annotation --> <context:include-filtertype="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/ > <! -- Do not scan Service --> <context:exclude-filtertype="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <! Default annotation mapping support, org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping --> <mvc:annotation-driven> <mvc:message-converters> <ref bean="fastJsonHttpMessageConverter" />
		<ref bean="stringHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <! MVC :interceptors> <! -- Internationalization --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/ > <! -- exception --> <bean class="com.xxx.common.interceptor.ExceptionInterceptor"/ > <! -- user Session --> <bean class="com.xxx.web.interceptor.UserLoginInterceptor"/> </mvc:interceptors> <! -- upload limit --> <beanid="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize" value="5242880" />
	<property name="maxInMemorySize" value="4096" />
	<property name="defaultEncoding" value="utf-8"/ > <! -- Delay parsing file --> <property name="resolveLazily" value="true"/> </bean> <! Freemark View analyzer --> <bean ID ="freemarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
	<property name="cache" value="true" />
	<property name="prefix" value="/views/" />
	<property name="suffix" value=".html" />
	<property name="contentType" value="text/html; charset=UTF-8" />
	<property name="exposeRequestAttributes" value="true" />
	<property name="exposeSessionAttributes" value="true" />
	<property name="exposeSpringMacroHelpers" value="true" />
	<property name="requestContextAttribute" value="request" />
</bean>
Copy the code

Special beans

Special beans used in DispatcherServlet: DispatcherServlet uses WebApplicationContext as its default context. Special beans in this context are:

Controller: the processor/page Controller that does the C thing in MVC, but the control logic has been moved to the front Controller to handle requests; If the mapping is successful, a HandlerExecutionChain object (containing a Handler Handler (page controller) object and multiple HandlerInterceptor interceptors) is returned. Such as BeanNameUrlHandlerMapping URL and Bean name mapping, mapping to the success of the Bean is the processor; 3. HandlerAdapter: The HandlerAdapter will wrap the processor as an adapter to support many types of processors. The application of the adapter design pattern makes it easy to support many types of processors. Such as SimpleControllerHandlerAdapter will Bean of implemented Controller interface adapter, and according to the CPU handleRequest method of function; ViewResolver: The ViewResolver resolves the logical View name into a specific View. With this strategy mode, it is easy to replace other View technologies. Such as InternalResourceViewResolver will logic view name mapped to the JSP view; 5. LocalResover: Localization resolution, because Spring supports internationalization, LocalResover resolves the Locale information of the client to facilitate internationalization. 6, ThemeResovler: theme analysis, through which to achieve a page multiple sets of styles, that is, common similar to the software skin effect; 7, MultipartResolver: file upload resolution, used to support file upload; 8, HandlerExceptionResolver: processor exception resolution, can map the exception to the corresponding unified error interface, so as to display the user-friendly interface (rather than give the user to see the specific error information); 9, RequestToViewNameTranslator: when the processor does not return relevant information, such as logical view name automatically request URL mapping for logical view name;

Now that this article is over, there is time for more details on initialization and implementation of the Dispatcher Servlet. Part of the article refers to Baidu Encyclopedia, if there is a problem, please leave a message pointed out.

Scan the qr code below and follow my official account!!