Spring Mvc:handleradapter

Source: Internet
Author: User
Tags mutex

Handlermapping This component, it 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.

Duty

After 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 Interfacehandleradapter{ Public Abstract Booleansupports (Object paramobject);  Public AbstractModelandview handle (httpservletrequest paramhttpservletrequest, HttpServletResponse paramhttpservletresponse, Object paramobject)throwsException;  Public Abstract Longgetlastmodified (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.

Annotationmethodhandleradpater

Supports method implementation

Which 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 Booleansupports (Object handler) {returnGetmethodresolver (handler). Hashandlermethods (); }  Privateservlethandlermethodresolver Getmethodresolver (Object handler) {Class Handlerclass=Classutils.getuserclass (handler); Servlethandlermethodresolver Resolver= This. Methodresolvercache.get (Handlerclass); if(Resolver = =NULL) {Resolver=NewServlethandlermethodresolver (Handlerclass);  This. Methodresolvercache.put (Handlerclass, resolver); }      returnResolver; }  

Handle method implementation

 PublicModelandview handle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {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) {returnInvokehandlermethod (Request, response, handler); }              }          }            returnInvokehandlermethod (Request, response, handler); } 

The focus of this approach is Invokehandlermethod

protectedModelandview Invokehandlermethod (httpservletrequest request, httpservletresponse response, Object handler) throwsException {servlethandlermethodresolver methodresolver=Getmethodresolver (handler); Method Handlermethod=Methodresolver.resolvehandlermethod (Request); Servlethandlermethodinvoker MethodInvoker=NewServlethandlermethodinvoker (Methodresolver); Servletwebrequest webRequest=Newservletwebrequest (request, response); Extendedmodelmap Implicitmodel=NewBindingawaremodelmap (); 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); returnMav; }  
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.
Summary : Handleradapter The function of this class is to take the handlermapping parse request to get the handler object. In more precise positioning to the method that can execute the request.

Reference:

http://blog.csdn.net/zhuojiajin/article/details/46597687

 

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.