Ssm-springmvc-17:springmvc Deep Anatomy Handleradapter Processor Adapter Bottom

Source: Internet
Author: User
Tags mutex

put a picture first

Very familiar with Ah, have seen before, we have already put handlermapping the bottom of the sky, along with the progress of the last, continue to follow, the Handleradapter processor adapter split

It is also dispatched by the Central scheduler Dispatcherservlet, so, or in this class to find Dodispatch (Request,response) This method, I put the useful code, stripped down

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; }//That's what I'm talking about. Handlemapping Execution Flow//so start with the first line in the back.Handleradapter ex= This. Gethandleradapter (Mappedhandler.gethandler ());

Handlerexecutionchain Mappedhandler This variable, is the processor execution chain, and its GetHandler method is to get the processor inside it
It's this.gethandleradpter outside.
DOT Past Tracking
    protectedhandleradapter Gethandleradapter (Object handler) throws Servletexception {//iteratorsIterator var2 = This. Handleradapters.iterator ();//the adapter object to return toHandleradapter ha;  Do {//If there is no next. Just throw the anomaly .            if(!Var2.hasnext ()) {                Throw NewServletexception ("No Adapter for handler ["+ Handler +"]: The Dispatcherservlet configuration needs to include a handleradapter it supports this handler"); }//turn this strong to a processor adapterHa =(Handleradapter) var2.next (); if( This. logger.istraceenabled ()) {                 This. Logger.trace ("testing Handler Adapter ["+ ha +"]"); }        }  while(!Ha.supports (handler));//There's an adapter pattern here .        returnha; }

This is the most intuitive source code after downloading the source code

    protectedhandleradapter Gethandleradapter (Object handler) throws Servletexception { for(Handleradapter ha: This. Handleradapters) {            if(logger.istraceenabled ()) {Logger.trace ("testing Handler Adapter ["+ ha +"]"); }            if(Ha.supports (handler)) {returnha; }        }        Throw NewServletexception ("No Adapter for handler ["+ Handler +"]: The Dispatcherservlet configuration needs to include a handleradapter it supports this handler"); }

See Ha.sipports (Handler) This method

Find the Handleradapter interface

 Public Interface Handleradapter {    Boolean supports (Object handler);    Modelandview handle (HttpServletRequest request, httpservletresponse response, Object handler) throws Exception;     Long getlastmodified (httpservletrequest request, Object handler);}

Take a look at the controller you wrote. This looks at the Handlerapater in the Dispatcherservlet property who supports the controller type of processor. During the run, Simplecontrollerhandleradapter is found to support controller type controllers.
Take a look at the Simplecontrollerhandleradapter code.

Find its implementation class: Ctrl+h simplecontrollerhandleradapter

 Public classSimplecontrollerhandleradapter implements Handleradapter { PublicBoolean supports (Object handler) {return(handler instanceof Controller); }         PublicModelandview handle (httpservletrequest request, httpservletresponse response, Object handler) throws Exc eption {return( Controller) handler). HandleRequest (request, response); }         Public Longgetlastmodified (httpservletrequest request, Object handler) {if(Handler instanceof lastmodified) {return((lastmodified) handler). getlastmodified (Request); }          return-1L; }    }  

IT support to determine whether the processor

Get the processor, call the handle method in a central scheduler.

    protected voidDodispatch (httpservletrequest request, httpservletresponse response) throws Exception {HttpServletRequest Pro Cessedrequest=request; Handlerexecutionchain Mappedhandler=NULL; Boolean multipartrequestparsed=false; Webasyncmanager Asyncmanager=Webasyncutils.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); 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)) {                    LongLastModified =ha.getlastmodified (Request, Mappedhandler.gethandler ()); if(logger.isdebugenabled ()) {Logger.debug ("last-modified value for ["+ Getrequesturi (Request) +"] is:"+lastmodified); }                    if(NewServletwebrequest (Request, Response). Checknotmodified (lastmodified) &&isget) {                        return; }                }                if(!Mappedhandler.applyprehandle (processedrequest, response)) {                    return; }                //actually invoke the handler.MV =Ha.handle (processedrequest, Response, Mappedhandler.gethandler ());//This is the place above .

Look at the handle method because the call is executed, and it returns Modelandview

The handlerequest inside the return walk is the controller interface, looking for its implementation class

Abstractcontroller abstract class, it has implemented HandleRequest

@Override PublicModelandview HandleRequest (httpservletrequest request, httpservletresponse response) throws Exception { //Delegate to Webcontentgenerator for checking and preparing.checkrequest (Request);        Prepareresponse (response); //Execute handlerequestinternal in synchronized block if required.        if( This. Synchronizeonsession) {HttpSession session= Request.getsession (false); if(Session! =NULL) {Object Mutex=Webutils.getsessionmutex (session); Synchronized (mutex) {returnhandlerequestinternal (request, response); }            }        }        returnhandlerequestinternal (request, response); }

See that return value on the last side? Is the Modelandview object, which calls the Handlerequestinternal (Request,respone) method

Remember Abstractcontroller 's implementation class? It's not a processor. The way to define it is to inherit Abstractcontroller

Package Cn.dawn.day04abstractcontroller;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.abstractcontroller;import Org.springframework.web.servlet.mvc.Controller; Import Javax.servlet.http.httpservletrequest;import javax.servlet.http.HttpServletResponse;/** * Created by Dawn on 2018/3/19.*///Processor Public classFirstcontroller extends Abstractcontroller {protectedModelandview handlerequestinternal (httpservletrequest httpservletrequest, HttpServletResponse HttpServletResponse) throws Exception {Modelandview me=NewModelandview (); Me.setviewname ("Index"); returnme; }}

Its implementation class default requirement must implement Handlerequestinternal (Request,response) This method

So, clear it up, step back and return to the central scheduler.

conclusion, the CPU Dispatcherservlet through the processor execution chain to find the Handleradapter processor adapter, which dispatched its processor, through the processor processing, return a Modelandview model and view objects, All the way back to the central scheduler

Ssm-springmvc-17:springmvc Deep Anatomy Handleradapter Processor Adapter Bottom

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.