SPRINGMVC Dispatchservlet initialization of nine load Strategies (ii)

Source: Internet
Author: User

4. Inithandlermappings Request for distribution

Handlermappings is a list

org.springframework.web.servlet.handlermapping= Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping, Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

It is known that the beannameurlhandlermapping and defaultannotationhandlermapping, which are initialized by default, are mapping:

Defaultannotationhandlermapping

(spring3.1 is obsolete) currently uses requestmappinghandlermapping instead, for annotation @controller, @RequestMapping to define the Controller, The main function is to map the controller's annotation values (Classpath and method paths) to the controller instance.

Beannameurlhandlermapping

Mapping a URL to a controller through a configuration file

The Handlermapping family has two branches, namely Abstracturlhandlermapping and abstracthandlermethodmapping, which are also inherited from abstracthandlermapping , their function is briefly described as follows:

    1. Abstracturlhandlermapping: Link URLs to handler by matching URLs
    2. Abstracthandlermethodmapping: Commonly used for @requestmaping, matching content takes its method as a handler

The Handermapping interface has only one method GetHandler (request), which is used to obtain Handlerexecutionchain, The Handlerexecutionchain object encapsulates a handler processing object and some interceptors, which means that each request is executed hander the logic associated with these interceptors. The specific knowledge about handler and interceptor interceptor can be referred to the handler and interceptor of Handlerexecutionchain in SPRINGMVC

5. Inithandleradapters Request Handler Processor

Like Handlermappings, Handleradapters is a list-type data, with the default handleradapters of Simplecontrollerhandleradapter, Annotationmethodhandleradapter, as follows:

Org.springframework.web.servlet.handleradapter=org.springframework.web.servlet.mvc.httprequesthandleradapter, Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter, Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

Springmvc in the handler to find support for its handleradapter, find handler corresponding Handleradapter, processing this hander (that is, processing the request). One of the annotationmethodhandleradapter has been abolished.

"Handleradapter literally means to handle an adapter, and its role in a nutshell is to invoke specific methods to handle requests from users." When Handlermapping gets to the controller that executes the request, Dispatcherservlte calls the corresponding Handleradapter according to the controller type that the controller corresponds to for processing. "-- the Handleradapter analysis of Springmvc "

The 3 methods in the Handleradapter interface are as follows:

Whether the current handleradapter supports this handlerboolean supports (Object handler); Processes the handler request and returns a View object Modelandviewmodelandview handle (httpservletrequest req, httpservletresponse res, object handler); Gets the last modified time long getlastmodified (httpservletrequest request, Object handler);

There are 6 classes in Spingmvc 4.3 that implement the Handleradapter interface, where Annotationmethodhandleradapter has been abolished and 5 are left:

    1. Abstracthandlermethodadapter--(easy to expand,)
    2. Requestmappinghandleradapter--can be executed as a Hadnlermethod type Handler
    3. Httprequesthandleradapter--can perform httprequesthandler types of Handler
    4. Simplecontrollerhandleradapter--can perform a Controller type of Handler
    5. Simpleservlethandleradapter--can perform Handler of the Servlet type

a myth : because the common in the project is often the processor is configured to do, it is easy to assume that the Controller interface is the interface of all processors, only the controller in the eye. A processor is not the only controller. There are also processors such as Httprequesthandler,servlet.

requestmappinghandlermapping will add the @requestmapping annotations in the controller to a container, Then Requestmappinghandleradapter can do some extra processing on the data requested by these methods based on the custom configuration inside.

Simplecontrollerhanderadapter mainly used for the handlerequest of the custom controller interface, we remember that when we first learned the controller, the custom controller either inherited Abstractcontroller, Either implements the Controller interface.

Httprequesthandleradapter handles Httprequesthandler type of handler, such as: SPRINGMVC in the request through Dispatcherservlet can not find resources, when the default servlet is set, will be handled by Defaultservlethttprequesthandler.

Refer to "Spring MVC static Resource Processing"

<servlet-mapping>         <servlet-name>default</servlet-name>        <url-pattern>*.jpg</ url-pattern>       </servlet-mapping>       <servlet-mapping>              <servlet-name>default</ servlet-name>             <url-pattern>*.css</url-pattern>        </servlet-mapping>

Simpleservlethandleradapter (to be added)

6. Inithandlerexceptionresolvers Request exception

Official documentation roughly describes:

Interface to being implemented by objects that can resolve exceptions thrown during handler mapping or execution, in the Typi Cal Case-to-error views. Implementors is typically registered as beans in the application context. Error views is analogous to JSP error pages but can is used with any kind of exception including any checked exception, W ITH potentially fine-grained mappings for specific handlers.

An interface implemented by an object that resolves exceptions thrown during handler mapping or execution, especially when view resolution is an exception error. Handlerexceptionresolver implementations are typically registered as beans in the context of an application. The error view is similar to a JSP error page, but can be used with any type of exception, including any checked exceptions, and a potential fine-grained mapping for a particular handler.

SPRINGMVC Dispatchservlet initialization of nine load Strategies (ii)

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.