Spring MVC Source Analysis (cont.)--Request processing

Source: Internet
Author: User

(Transfer position, Time Tunnel: http://www.jmatrix.org/spring/501.html)

The initialization of spring MVC is analyzed in the previous Spring MVC implementation Analysis-initialization process, and the request processing is then analyzed.

When looking for the entrance to the request processing, we need to know the programming specifications of the servlet, corresponding to different requests (such as post, get, etc.) implementation methods in Frameworkservlet, respectively, is Dopost, doget, and so on, see the concrete implementation of this series of methods can be known, The processing of the request jumps to the ProcessRequest function, and finally into the Dispatcherservlet doservice function, the detailed flow such as:



The sequence diagram above shows a detailed request processing flow, the most important of which is the Dodispatch function, which contains the entire processing logic,

protected void Dodispatch (HttpServletRequest request, httpservletresponse response) throws Exception {Httpservletreque
		St Processedrequest = Request;
		Handlerexecutionchain mappedhandler = null;

		Boolean multipartrequestparsed = false;

		Webasyncmanager Asyncmanager = Asyncwebutils.getasyncmanager (request);
			try {modelandview mv = null;

			Exception dispatchexception = null;
				try {processedrequest = Checkmultipart (request);

				multipartrequestparsed = processedrequest! = Request;
				Determine handler for the current request.
				Mappedhandler = GetHandler (Processedrequest, false);
					if (Mappedhandler = = NULL | | Mappedhandler.gethandler () = = null) {Nohandlerfound (processedrequest, response);
				Return
				}//Determine handler adapter for the current request.

				Handleradapter ha = Gethandleradapter (Mappedhandler.gethandler ());
				Process last-modified Header, if supported by the handler. ... try {//actually invoke the handler. mv = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ());
					} finally {if (asyncmanager.isconcurrenthandlingstarted ()) {return;
				}} applydefaultviewname (request, MV);
			Mappedhandler.applyposthandle (processedrequest, response, MV);
			} catch (Exception ex) {dispatchexception = ex;
		} processdispatchresult (Processedrequest, Response, Mappedhandler, MV, dispatchexception);
		} ...... }
	Finally {...} }

First, let's look at some of the handlerexecutionchain in this class.

public class Handlerexecutionchain {

	private final Object handler;

	Private handlerinterceptor[] interceptors;

	Private list

There are two key things here. Handler and interceptors, also known as the Processor object and the Interceptor chain list, relate to our actual programming, handler there may be two things: (1) Controller class object ; (2) The method object in the controller (the term may be less accurate ...). )。 Interceptors also defines a list of interceptor objects, and if you want to do something before the request is processed, you'll get one. With this in mind, take a look at the Dodispatch function if you get the Handlerexecutionchain object Mappedhandler.

Can see actually now in the GetHandler method:

Protected Handlerexecutionchain GetHandler (HttpServletRequest request) throws Exception {for
		(handlermapping HM: this.handlermappings) {
			...
			Handlerexecutionchain handler = Hm.gethandler (request);
			if (handler! = null) {
				return handler;
			}
		}
		return null;
	}

The implementation of the function is very simple: traverse the registered handlermapping list and get the Handlerexecutionchain object through the GetHandler function of the Handlermapping object. As for the GetHandler function in handlermapping How to get the processor and interceptor required by Handlerexecutionchain, I can only say that the process is cumbersome, the principle is simple, For processor objects (handler), depending on the mapping implementation, they can be found based on URLPath or method annotations in the bean configuration, which is not described here.

Following the execution flow of the Dodispatch function, the next step is to get the Handleradapter object ha through the Gethandleradapter function, and then a call to take care of it again:

MV = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ());

With the call to Handleradapter's handle function, Spring MVC begins to really "do something", and so-called "doing things" begins to invoke the controller (control logic) we wrote. This process is analyzed by the implementation of the two Handleradapter Httprequesthandleradapter and Annotationmethodhandleradapter, in a previous article "Spring MVC Implementation Analysis --The initialization process "has a reference to my own spring MVC program configuration, which will be expanded as follows:

The implementation of Httprequesthandleradapter is the easiest to understand, because its handle implementation is called the processor (handler) object HandleRequest function, with F4 to see the controller's inheritance system, Then look at the implementation of the HandleRequest function in Abstractcontroller, combined with the spring MVC "Hello World Program" written by your rookie, you will have a sense of epiphany ⊙﹏⊙.

As for annotationmethodhandleradapter, its implementation principle is also very easy to understand, we already know that it is for the use of annotated method mapping, practical applications such as:

@RequestMapping (method=requestmethod.get,value= "/homecontroller.xhtml")
	protected Modelandview Handlerequestinternal (HttpServletRequest arg0,
			httpservletresponse arg1) throws Exception {
      ...
}

So the realization of its handle is to find the corresponding processing method of annotations through the reflection mechanism of Java, and invoke the execution of the control logic.

At this time, let us go back to the dodispatch process, after the handle "dry things", we get the Modelandview object, that is, the view object, it is natural that the next is the view of rendering and display, which is our final point to analyze. The entry is a function call in Dodispatch:

Processdispatchresult (processedrequest, Response, Mappedhandler, MV, dispatchexception);




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.