DispatcherServlet forwarder of Spring MVC 4

Source: Internet
Author: User

DispatcherServlet forwarder of Spring MVC 4

Similar to other MVC frameworks, Spring MVC 4 is request-driven. It uses a central Servlet processor to forward requests to the Controller and then implement relevant functions. Different from other MVC frameworks, Spring's DispatcherServlet integrates with Spring's IoC container and allows users to use other Spring features.


1. Functions of DispatcherServlet


Shows the workflow of Spring MVC 4. DispatcherServlet acts as a front-end controller.



DispatcherServlet is actually a Servlet that inherits HttpServlet. It must be declared in web. xml before use. Configure url ing according to the request to be processed by DispatcherServlet. The declaration format is as follows:


     
          
   
    favsoft
           
   
    org.springframework.web.servlet.DispatcherServlet
           
   
    1
       
       
          
   
    favsoft
         
  
 
       
 
  /favsoft/*
     
   
 


If we follow the above DispatcherServlet, all requests starting with/favsoft will be forwarded by the DispatcherServlet instance in the request URL. You can also configure the Servlet container programmatically in the environment after Servlet 3.0, as shown in, which is equivalent to the Code configured in web. xml.


public class MyWebApplicationInitializer implements WebApplicationInitializer {    
@Override public void onStartup(ServletContext container) { ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet()); registration.setLoadOnStartup(1); registration.addMapping("/favsoft/*");
} }


WebApplicationInitializerIs an interface provided by Spring MVC to identify the configuration in the Code and initialize the Servlet container.AbstractDispatcherServletInitializerAbstract classes implement this interface so that the class is registered to DispatcherServlet through servlet ing.


In the Spring MVC framework, each DispatcherServlet has its WebApplicationContext context, which inherits all classes (Java Beans) in the root context of WebApplicationContext, and these inherited classes (Java Beans) it can be rewritten in a specific servlet scope or define a class instance in a new scope.


2. Default DispatcherServlet Configuration


By default, DispatcherServlet is configured according to DispatcherServlet. properties under org. springframework. web. servlet.


# Default implementation classes for DispatcherServlet's strategy interfaces.

# Used as fallback when no matching beans are found in the DispatcherServlet context.

# Not meant to be customized by application developers.

Org. springframework. web. servlet. LocaleResolver = org. springframework. web. servlet. i18n. AcceptHeaderLocaleResolver

Org. springframework. web. servlet. ThemeResolver = org. springframework. web. servlet. theme. FixedThemeResolver

Org. springframework. web. servlet. HandlerMapping = org. springframework. web. servlet. handler. BeanNameUrlHandlerMapping ,\

Org. springframework. web. servlet. mvc. annotation. DefaultAnnotationHandlerMapping

Org. springframework. web. servlet. HandlerAdapter = org. springframework. web. servlet. mvc. HttpRequestHandlerAdapter ,\

Org. springframework. web. servlet. mvc. SimpleControllerHandlerAdapter ,\

Org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter

Org. springframework. web. servlet. HandlerExceptionResolver = org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerExceptionResolver ,\

Org. springframework. web. servlet. mvc. annotation. ResponseStatusExceptionResolver ,\

Org. springframework. web. servlet. mvc. support. DefaultHandlerExceptionResolver

Org. springframework. web. servlet. RequestToViewNameTranslator = org. springframework. web. servlet. view. DefaultRequestToViewNameTranslator

Org. springframework. web. servlet. ViewResolver = org. springframework. web. servlet. view. InternalResourceViewResolver

Org. springframework. web. servlet. FlashMapManager = org. springframework. web. servlet. support. SessionFlashMapManager

3. workflow of DispatcherServlet


After DispatcherServlet is configured and a specific DispatcherServlet request is sent, the workflow is as follows:


(1) The WebApplicationContext context seeks and binds the controller and the default value under DispatcherServlet. WEB_APPLICATION_CONTEXT_ATTRIBUTE by default.


(2) The language parser is bound to elements in the request startup process to handle requests (rendering views, preparing data, etc.) in the language environment in use. If you do not need to process internationalization, this step is not required.


(3) The theme parser binds the request to let the elements like the view know which topic to use. If the topic is not used, ignore this step.


(4) If a multi-file parser is specified, the request will check these files. If these files are found, the request will be packaged into MultipartHttpServletRequest to further process other elements.


(5) Next, find a suitable processor. If the processor is found, the execution chain related to the processor will be executed to prepare for the data model or rendering view.


(6) If a model is returned, the view is rendered. If no response model is returned (the preprocessing or post-processing program intercepts the request for security reasons), the view does not need to be rendered and the request may have been completed.


If an exception occurs during request processing, it will be caught and thrown by the exception processor in the WebApplicationContext context. Of course, the exception processor allows us to handle exceptions through some custom behaviors.


Spring DispatcherServlet also supports returning the latest modification date: DispatcherServlet searches for a suitable processor and tests whether the processor has implemented the latest update interface. If it passes through, the LashModified interface returns the long type value to the client.


The DispatcherServlet instance can be customized by configuring initialization parameters in web. xml. The following parameters can be declared in web. xml:


ContextClass The WebApplicationContext interface is implemented. XMLWebApplicationContext is used by default.
ContextConfigLocation Specifies the location of the used context. Multiple contexts are supported. If the same context is defined multiple times, the recently defined context is used.
Namespace The namespace of WebApplicationContext. The default value is servlet-name-servlet.



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.