Spring mvc--processing the request process

Source: Internet
Author: User

1. Overview

The following diagram is the overall flow of Spring MVC processing requests, where front controller refers to the Dispatcherservlet class.

1. All client requests are sent to Dispatcherservlet and processed by Dispacherservlet.

2.DispacherServlet distributes the request to different controller classes, and the controller class handles the corresponding request.

When the 3.Controller class finishes processing the corresponding request, the corresponding model or view information will be processed to the Dispatcherservlet class.

4.DispatcherServlet this information to the view template parsing.

5. View template resolution to the corresponding view information, etc. to Dispacherservlet.

6. Finally, the view information is pushed to the client by Dispacherservlet.

As a result, it can be seen that the Dispatcherservlet class occupies a very important place in the Spring MVC framework, which receives all requests, distributes the corresponding requests to the corresponding feature class instances, and responds to the client with the processed results.

2. Dispatcher Distribution Processing Request

In the subclass Frameworkservlet of Dispatcherservlet, you can see that it has rewritten the corresponding method of all the request methods of the parent class HttpServlet. These methods finally call the ProcessRequest method to process the request, which makes a simple processing of the request and then calls the Doservice method, which is implemented by the parent class Dispatcherservlet.

In the Doservice method of the Dispatcherservlet class, some corresponding attribute instances are first set for request requests, and then the Dodispatch method is called to distribute processing requests. The following is the actual processing process

1. In the Dodispatch method, call the Checkmultipart method to determine whether the request is a multipart request (such as a file upload), and if so, use the Multipartresolver instance to convert the request to a multipart request.

2. Call the GetHandler method to get the Handlerexecutionchain instance object. In this method, we first find the handlermapping instance that conforms to the current one, and then call the GetHandler method of the Handlermapping instance to get the Handlerexecutionchain instance. The instance wraps the Handlermapping instance object and sets a matching interceptor in the instance.

3. Call the Gethandleradapter method to find the corresponding Handleradapter adapter instance for the current handlermapping instance.

4. Call the Applyprehandler method of the Handlerexecutionchain instance to invoke its interceptor's Prehandler method (the pre-intercept method) execution. If True, the process is resumed, otherwise the method call processing is stopped. (Pre-intercept method execution Call)

5. Call the handler method of the Handleradapter adapter instance to process the current request and return the corresponding Modelandview object. ( call the corresponding method of the target object to execute)

6. Call the Applyposthandler method of the Handlerexecutionchain instance to invoke the Posthandler method of its interceptor (the post-intercept method) execution. ( post-intercept method execution Call)

7. Call the Processdispatchresult method to process the final result (Modelandview or exception), which invokes the Render method to render the parsed final view. In the Render method, first through the Viewresolver parser to obtain the corresponding view name of the View object, and finally call the corresponding View object render method rendering processing view, and respond to the corresponding results to the client. ( call the corresponding view rendering views)

3. Summary

As above, the Dispatcherservlet class actually distributes processing requests primarily with the following important steps. Wraps the handlermapping and corresponding interceptors in a Handlerexecution instance object, then obtains the Handleradapter adapter that the handlermapping wants to match, and then calls the pre-interceptor method, and if True is returned, The adapter is called to process the current request, and the corresponding Modelandview object is returned, and then the Interceptor method is called, and finally the Viewresolver view parser is called to parse the corresponding view object, and the view object is called to render the views and respond to the corresponding results to the client.

The following are the important interfaces and classes that spring MVC handles requests.

Handlermapping interface, Handlerinterceptor Interceptor interface and its wrapper class Mappedinterceptor, Handlerexecutionchain class, Handleradapter Adapter interface, Viewresolver View resolver interface, view interface, Httpmessageconvert message translation interface

Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5

"Java Framework source code download"

Spring mvc--processing the request process

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.