Ssm-springmvc-04:springmvc Understanding handlemapping (source analysis)

Source: Internet
Author: User

------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------

First from the conceptual understanding, from the central scheduler, carrying the parameter request, dispatching to the Handlemapping processor mapper, the processor mapper returns the processor execution chain to the central scheduler

I walk through the bottom to confirm the concept:

1. All said to be a central dispatcher, so find the central scheduler first Dispatcherservlet

2. Find a way from him ctrl+f (Dodistch)

    protected voidDodispatch (httpservletrequest request, httpservletresponse response) throws Exception {HttpServletRequest Pro Cessedrequest=request; Handlerexecutionchain Mappedhandler=NULL; Boolean multipartrequestparsed=false; Webasyncmanager Asyncmanager=Webasyncutils.getasyncmanager (Request); Try {            Try{Modelandview Err=NULL; Exception dispatchexception=NULL; Try{processedrequest= This. Checkmultipart (Request); Multipartrequestparsed= processedrequest! =request; Mappedhandler= This. GetHandler (Processedrequest); if(Mappedhandler = =NULL|| Mappedhandler.gethandler () = =NULL) {                         This. Nohandlerfound (processedrequest, response); return; } Handleradapter ex= This. Gethandleradapter (Mappedhandler.gethandler ()); String Method=Request.getmethod (); Boolean Isget="GET". Equals (method); if(Isget | |"HEAD". Equals (method)) {                        LongLastModified =ex.getlastmodified (Request, Mappedhandler.gethandler ()); if( This. logger.isdebugenabled ()) {                             This. Logger.debug ("last-modified value for ["+ Getrequesturi (Request) +"] is:"+lastmodified); }                        if((NewServletwebrequest (Request, Response)). Checknotmodified (lastmodified) &&isget) {                            return; }                    }                    if(!Mappedhandler.applyprehandle (processedrequest, response)) {                        return; } Err=Ex.handle (processedrequest, Response, Mappedhandler.gethandler ()); if(asyncmanager.isconcurrenthandlingstarted ()) {return; }                     This. Applydefaultviewname (Processedrequest, err);                Mappedhandler.applyposthandle (processedrequest, response, err); } Catch(Exception var19) {dispatchexception=Var19; }                 This. Processdispatchresult (processedrequest, Response, Mappedhandler, err, dispatchexception); } Catch(Exception var20) { This. Triggeraftercompletion (processedrequest, Response, Mappedhandler, VAR20); } Catch(Error var21) { This. Triggeraftercompletionwitherror (processedrequest, Response, Mappedhandler, VAR21); }        } finally {            if(asyncmanager.isconcurrenthandlingstarted ()) {if(Mappedhandler! =NULL) {mappedhandler.applyafterconcurrenthandlingstarted (processedrequest, response); }            } Else if(multipartrequestparsed) { This. Cleanupmultipart (Processedrequest); }        }    }

A lot of the content of this method does not need attention, need to pay attention to my talk about

HttpServletRequest processedrequest =request; //receiving RequestsHandlerexecutionchain Mappedhandler =NULL; //Processor execution ChainBoolean multipartrequestparsed =false; //multipart request, file uploadWebasyncmanager Asyncmanager =Webasyncutils.getasyncmanager (Request); //request for a different part        Try {            Try{Modelandview Err=NULL; //View Resolution                Try{processedrequest= This. Checkmultipart (Request); //Check if a multipart requestmultipartrequestparsed = processedrequest! =request; Mappedhandler= This. GetHandler (Processedrequest); //This returns the processor execution chain

3. Here, click GetHandler (processedrequest) to view

    protectedhandlerexecutionchain gethandler (HttpServletRequest request) throws Exception {Iterator var2= This. Handlermappings.iterator ();        Handlerexecutionchain handler;  Do {            if(!Var2.hasnext ()) {                return NULL; } handlermapping HM=(handlermapping) var2.next (); if( This. logger.istraceenabled ()) {                 This. Logger.trace ("testing handler Map ["+ HM +"] in Dispatcherservlet with name \ '"+ This. Getservletname () +"\ '"); } handler=Hm.gethandler (Request); }  while(Handler = =NULL); returnhandler; }

I'm refining the key code to explain a wave.

        //iterators, not generics, Handlermapping is a list collectionIterator var2 = This. Handlermappings.iterator (); //Processor execution ChainHandlerexecutionchain Handler;  Do {            if(!Var2.hasnext ()) {                return NULL; }            //Processor MapperHandlermapping HM =(handlermapping) var2.next (); //Keep Tracking .Handler =Hm.gethandler (Request); }  while(Handler = =NULL); returnHandler

4. Trace Hm.gethandler (Request) method, found that he is the Handlermapping interface, (CTRL+H) find its implementation class Abstracthandlermapping,ctrl+f find GetHandler

     Publicfinal Handlerexecutionchain gethandler (HttpServletRequest request) throws Exception {Object handler= This. gethandlerinternal (Request); if(Handler = =NULL) {Handler= This. Getdefaulthandler (); }        if(Handler = =NULL) {            return NULL; } Else {            if(Handler instanceof string) {string Executionchain=(String) handler; Handler= This. Getapplicationcontext (). Getbean (Executionchain); } handlerexecutionchain executionChain1= This. Gethandlerexecutionchain (handler, request); if(Corsutils.iscorsrequest (Request)) {corsconfiguration GlobalConfig= This. corsconfigsource.getcorsconfiguration (Request); Corsconfiguration Handlerconfig= This. Getcorsconfiguration (handler, request); Corsconfiguration Config= GlobalConfig! =NULL?Globalconfig.combine (handlerconfig): Handlerconfig; ExecutionChain1= This. Getcorshandlerexecutionchain (Request, executionChain1, config); }            returnexecutionChain1; }    }

Again, I put the key code to explain a wave

//Get processorObject handler = This. gethandlerinternal (Request); //The processor is empty, with the default        if(Handler = =NULL) {Handler= This. Getdefaulthandler (); }        //The default is also null to return null        if(Handler = =NULL) {            return NULL; } Else {            //determine if it is a string type            if(Handler instanceof String) {//This is the ID of the processor from which we are generally using the From configuration file BeanString handlerName = (string) handler;///hello//this is spring.Handler = This. Getapplicationcontext (). Getbean (HandlerName); }
Get handler execution chain
Handlerexecutionchain executionChain1 = This.gethandlerexecutionchain (handler, request);
    // when it returns, it becomes a portable processor.
  
return This . Gethandlerexecutionchain (handler, request);}

5. Follow-up of the acquisition program execution chain, This.gethandlerexecutionchain (handler,request)

    protectedhandlerexecutionchain Gethandlerexecutionchain (Object handler, HttpServletRequest request) {//ternary expression, is the processor execution chain on the strong turn, not on the acquisition based on the processor generated aHandlerexecutionchain chain = Handler instanceof Handlerexecutionchain? (Handlerexecutionchain) Handler:NewHandlerexecutionchain (handler); String Lookuppath= This. Urlpathhelper.getlookuppathforrequest (Request); Iterator VAR5= This. Adaptedinterceptors.iterator ();  while(Var5.hasnext ()) {//just know to add interceptors hereHandlerinterceptor Interceptor =(Handlerinterceptor) var5.next (); if(Interceptor instanceof Mappedinterceptor) {mappedinterceptor mappedinterceptor=(Mappedinterceptor) interceptor; if(Mappedinterceptor.matches (Lookuppath, This. Pathmatcher))                {Chain.addinterceptor (Mappedinterceptor.getinterceptor ()); }            } Else{chain.addinterceptor (Interceptor); }        }        returnchain; }

Ssm-springmvc-04:springmvc Understanding handlemapping (source 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.