Dispatcherservlet of Spring MVC 4

Source: Internet
Author: User

Spring MVC 4 is similar to other MVC frameworks in that it is request-driven and forwards the request to the Controller via the central servlet processor and completes the corresponding function. Unlike other MVC frameworks, Spring's Dispatcherservlet integrates with the spring IOC container and allows users to use other features of spring.


1. The role of Dispatcherservlet


The workflow for Spring MVC 4 is as shown. Dispatcherservlet acts as the "Front controller" role.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/53/B4/wKioL1Rt8_SB24kjAAImFxyJo2Q573.jpg "title=" Dispatcherservlet.png "alt=" Wkiol1rt8_sb24kjaaimfxyjo2q573.jpg "/>


Dispatcherservlet is actually a servlet that inherits the HttpServlet and must be declared in Web. XML before use, configuring the URL mappings according to the requests Dispatcherservlet need to process. The declaration format is as follows:

<web-app> <servlet> <servlet-name>favsoft</servlet-name> <servlet-class>org .springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>favsoft</servlet-name>
<url-pattern>/favsoft/*</url-pattern>
</servlet-mapping>
</web-app>


Assuming we follow the above Dispatcherservlet, all requests beginning with/favsoft in the requested URL will be forwarded by the Dispatcherservlet instance. In the context of Servlet 3.0, you can also programmatically configure the Servlet container as shown in the code that is 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/*");
}}

    webapplicationinitializer   is an interface provided by spring MVC to identify configuration in code and initialize the servlet container. abstractdispatcherservletinitializer   abstract class implements the interface so that the class is registered to Dispatcherservlet through the servlet mappings.


In the spring MVC framework, each dispatcherservlet has its webapplicationcontext context, which inherits all the classes in the Webapplicationcontext root context (Java Beans), these inherited classes (Java Beans) can be overridden in a specific servlet scope, or you can define a new scope class instance.


2. Default Dispatcherservlet Configuration


By default, Dispatcherservlet is configured according to the dispatcherservlet.properties under Org.springframework.web.servlet.


# Default Implementation classes for Dispatcherservlet ' s strategy interfaces.

# used as fallback when no matching beans is found in the Dispatcherservlet context.

# meant to being 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. Dispatcherservlet Workflow


After we have configured the Dispatcherservlet, a specific Dispatcherservlet request comes in, and its workflow is as follows:


(1) The Webapplicationcontext context seeks and binds the controller and, by default, binds the default value under Dispatcherservlet.web_application_context_attribute.


(2) The language parser binds to the elements in the request initiation process to resolve requests (render view, prepare data, etc.) that are used by the locale, and this step is not required if internationalization is not required.


(3) The theme resolver binding request allows elements such as views to know which theme to use, and if no theme is used, this step is also ignored.


(4) If a multi-file parser is specified, the request checks for these files, and if they are found, the request is wrapped in multiparthttpservletrequest to further process the other elements.


(5) Next, look for a suitable processor, if found, the processor-related execution chain will be executed to prepare the data model or render view.


(6) If the model is returned, the view is rendered. If the model is not returned (perhaps for security reasons, the preprocessing or post-processor intercepts the request), no rendering of the view is required and the request may have been completed.


If an exception occurs during the processing of the request, it is caught and thrown by the exception handler in the Webapplicationcontext context. Of course, the exception handler allows us to handle exceptions through some custom behavior.


Spring Dispatcherservlet also supports returning the last modified date: Dispatcherservlet Find the right processor and test whether the processor implements the most recently updated interface, and if so, the Lashmodified interface will return a long The value of the type to the client.


The Dispatcherservlet instance can be customized by configuring the initialization parameters in Web. XML, and the following parameters can be declared in Web. xml:


Contextclass The Webapplicationcontext interface is implemented and Xmlwebapplicationcontext is used by default.
Contextconfiglocation Indicates the context location used, supporting the use of multiple contexts. If the same context is defined more than once, the most recently defined one is used.
Namespace The Webapplicationcontext namespace, which defaults to "Servlet-name"-servlet.


This article is from the "Dust Wind with the Sky" blog, please be sure to keep this source http://favccxx.blog.51cto.com/2890523/1580301

Dispatcherservlet of Spring MVC 4

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.