SPRINGMVC Approximate flow analysis

Source: Internet
Author: User

The core method of Dispatcherservlet Dodispatch

protected void Dodispatch (HttpServletRequest request, httpservletresponse response) throws Exception { HttpServletRequest processedrequest = Request; Handlerexecutionchain Mappedhandler = Null;int Interceptorindex = -1;try {modelandview Mv;boolean errorView = false;try {p Rocessedrequest = Checkmultipart (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. String method = Request.getmethod (); Boolean isget = "GET". Equals (method); if (Isget | | "HEAD". Equals (method)) {Long lastmodified = ha.getlastmodified (Request, Mappedhandler.gethandler ()); if ( Logger.isdebugenabled ()) {String RequestUri = Urlpathhelper.getrequesturi (request); LoggeR.debug ("Last-modified value for [" + RequestUri + "] is:" + lastmodified);} if (new Servletwebrequest (request, Response). Checknotmodified (lastmodified) && isget) {return;}} Apply Prehandle methods of registered interceptors. Handlerinterceptor[] interceptors = mappedhandler.getinterceptors (), if (interceptors! = null) {for (int i = 0; i < inte Rceptors.length; i++) {Handlerinterceptor interceptor = interceptors[i];if (!interceptor.prehandle (processedrequest, Response, Mappedhandler.gethandler ())) {triggeraftercompletion (Mappedhandler, Interceptorindex, processedRequest, response, null); return;} Interceptorindex = i;}} Actually invoke the HANDLER.MV = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ());//Do we need view n Ame translation?if (mv! = null &&!mv.hasview ()) {Mv.setviewname (Getdefaultviewname (Request));} Apply Posthandle Methods of registered interceptors.if (interceptors! = null) {for (int i = interceptors.length-1; I >= 0; i--) {HanDlerinterceptor Interceptor = Interceptors[i];interceptor.posthandle (processedrequest, Response, Mappedhandler.gethandler (), MV);}}} catch (Modelandviewdefiningexception ex) {Logger.debug ("Modelandviewdefiningexception encountered", ex); mv = Ex.getmodelandview ();} catch (Exception ex) {Object handler = (Mappedhandler! = null? Mappedhandler.gethandler (): null); mv = Processhandlerexce Ption (processedrequest, response, Handler, ex); Errorview = (mv! = null);} Did the handler return a view to Render?if (mv! = null &&!mv.wascleared ()) {render (MV, Processedrequest, RESPO NSE); if (Errorview) {webutils.clearerrorrequestattributes (request);}} else {if (logger.isdebugenabled ()) {Logger.debug ("Null Modelandview returned to Dispatcherservlet with name '" + Getservle Tname () + "': assuming Handleradapter completed request handling");}} Trigger after-completion for Successful outcome.triggeraftercompletion (Mappedhandler, Interceptorindex, Processedrequest, response, NULL);} catch (Exception ex) {//Trigger after-completion for thrown exception.triggeraftercompletion (Mappedhandler, Interceptorindex, Processedrequest, response, ex); throw ex;} catch (Error err) {servletexception ex = new Nestedservletexception ("Handler processing Failed", err);//Trigger after-com Pletion for thrown exception.triggeraftercompletion (Mappedhandler, Interceptorindex, processedrequest, Response, ex); Throw ex;} finally {//clean up any resources used by a multipart request.if (processedrequest! = Request) {Cleanupmultipart (Processe drequest);}}}

Start the analysis from the method above:

SPRINGMVC handles the specified URL through a dispatcherservlet servlet, and Dispatcherservlet iterates over all handlermapping when a request URL is processed.

That is, in the code above

  Mappedhandler = GetHandler (Processedrequest, false);
Protected Handlerexecutionchain GetHandler (HttpServletRequest request) throws Exception {for (handlermapping HM: This.handlermappings) {if (logger.istraceenabled ()) {Logger.trace ("Testing handler map [" + HM + "] in Dispatcherservlet W ith name ' "+ getservletname () +" ' ");} Handlerexecutionchain handler = Hm.gethandler (Request), if (handler! = NULL) {return handler;}} return null;}

From the above code can be learned: iterate all the handlermapping when found the right to return immediately, the object returned is Handlerexecutionchain,

The Handlerexecutionchain object contains the controller that is mapped to and some intetceptor (interceptors). And then through
GetHandler () in Handlerexecutionchain () is the controller gets the corresponding to Handleradapter ()
Handleradapter contains a lot of processing, such as parameter resolution (the user can implement their own parameter resolution) data binding information
In the call
MV = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ()); The middle of the method contains an iteration
All interceptors in the Handlerexecutionchain,
Handleradapter's
Handle will call the target Controler method and return the corresponding to the view

SPRINGMVC Approximate flow analysis

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.