Spring Learning Summary (2.3)-spring Mvc:handleradapter

Source: Internet
Author: User
Tags prepare

The previous blog describes the Handlermapping component, which is responsible for locating the request processor handler. This is the second step in the SPRINGMVC processing process. Then, when positioned to handler, Dispatcherservlet will be given the handler to inform Handleradapter,handleradapter and then according to the request to locate the specific processing method of the request.
ResponsibilitiesAfter Handlermapping returns the controller instance that handles the request, a processing class that helps locate the specific request method is required, which is the Handleradapter,handleradapter processor adapter, Spring MVC actually invokes the handler function through Handleradapter. For example, Spring MVC autoenrollment Annotationmethodhandleradpater,handleradapter defines how the requested policy is handled by requesting a URL, Request the Requestmapping definition of method and processor, finalize which method of the processing class is used to process the request, and examine the parameters of the corresponding processing method of the processing class and the related annotation configuration, determine how to convert the required parameters into the calling method, And the final call returns Modelandview. After finding the corresponding handler method according to Handlermapping in Dispatcherservlet, first check all the available Handleradapter registered in the current project, Find the Handleradapter you can use based on the Supports method in Handleradapter. By invoking the handler method in Handleradapter to process and prepare the parameters and annotation of handler method (this is how spring MVC turns the parameters in request into handle method), and finally calls the actual handler method.
Handleradapter Interface
Public abstract Interface handleradapter{Public  Abstract Boolean supports (Object paramobject);    Public abstract Modelandview handle (HttpServletRequest paramhttpservletrequest, HttpServletResponse Paramhttpservletresponse, Object Paramobject)    throws Exception;    Public abstract long getlastmodified (HttpServletRequest paramhttpservletrequest, Object paramobject);}


The interface defines three methods, and the role of the support method is to determine whether the processing adapter supports the handler. Hanle method, invokes the appropriate method in the corresponding handler, and returns a Modelview. The third method is not sure what to do with.
Annotationmethodhandleradpatersupports method implementationWhich again called the Getmethodresolver this method is also involved in a lot of things, I do not see very understand not to go deeper to say. Its main logic is to obtain servlethandlermethodresolver according to the incoming handler, the function of this servlethandlermethodresolver can be simply considered as the method of Serlvet use for parsing.
Public Boolean supports (Object handler) {          return Getmethodresolver (handler). Hashandlermethods ();      }  Private Servlethandlermethodresolver Getmethodresolver (Object handler) {      Class Handlerclass = Classutils.getuserclass (handler);      Servlethandlermethodresolver resolver = this.methodResolverCache.get (handlerclass);      if (resolver = = null) {          resolver = new Servlethandlermethodresolver (handlerclass);          This.methodResolverCache.put (Handlerclass, resolver);      }      return resolver;  }  


Handl Method Implementation
Public Modelandview handle (httpservletrequest request, httpservletresponse response, Object handler) throws E              xception {if (Annotationutils.findannotation (Handler.getclass (), sessionattributes.class)! = null) {              Always prevent caching in case of Session attribute management.              Checkandprepare (Request, Response, this.cachesecondsforsessionattributehandlers, true);          Prepare cached set of session attributes names.              } else {//Uses configured default Cacheseconds setting.          Checkandprepare (Request, response, true);          }//Execute Invokehandlermethod in synchronized block if required.              if (this.synchronizeonsession) {HttpSession session = Request.getsession (false);                  if (session! = NULL) {Object mutex = Webutils.getsessionmutex (session); Synchronized (mutex) {return InvokehandlerMethod (Request, response, handler);      }}} return Invokehandlermethod (request, response, handler);  }


The focus of this approach is Invokehandlermethod
Protected Modelandview Invokehandlermethod (httpservletrequest request, httpservletresponse response, Object handler)          Throws Exception {Servlethandlermethodresolver methodresolver = Getmethodresolver (handler);          Method Handlermethod = Methodresolver.resolvehandlermethod (request);          Servlethandlermethodinvoker MethodInvoker = new Servlethandlermethodinvoker (methodresolver);          Servletwebrequest webRequest = new Servletwebrequest (request, response);            Extendedmodelmap Implicitmodel = new Bindingawaremodelmap ();          Object result = Methodinvoker.invokehandlermethod (Handlermethod, Handler, WebRequest, Implicitmodel); Modelandview Mav = Methodinvoker.getmodelandview (Handlermethod, Handler.getclass (), result, Implicitmodel          , webRequest);          Methodinvoker.updatemodelattributes (Handler, (MAV! = null? Mav.getmodel (): null), Implicitmodel, webRequest);      return MAV; }


In fact, the thing it does is to obtain the corresponding request in the handler executable method, or through the Getmethodresolver method to achieve. has been built in the Supports method, so it is good to go directly to the cache when the call is made here.
SummaryHandleradapter: The function of this class is to take over the handler object that the Handlermapping parse request obtains. In more precise positioning to the method that can execute the request.

Spring Learning Summary (2.3)-spring Mvc:handleradapter

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.