Spring Source Research: Data binding

Source: Internet
Author: User

When we do spring MVC, we simply use @controllor to mark the Controllor bean, and then use the @requestmapping ("tag") to mark the method that needs to accept the request. The first parameter in the method is the HttpServletRequest type, the last parameter is the model type, and the middle can be any pojo, as long as it conforms to the standard, there are set and get,spring that can be automatically bound to the Pojo object's property name according to the parameter name in the page request, This is quite handy. What is the principle of it? Look at the source code to know.

First of all, to know the method object's invoke (invoke, with the help) approach, look at this piece of code:

Class clazz = Class.forName ("Taskprovidepropslist");//The full name of the class hereObject obj = clazz.newinstance ();//get an instance of a classfield[] fields = Clazz.getdeclaredfields ();//Get Property List//Write Data       for(Field f:fields) {propertydescriptor pd=NewPropertyDescriptor (F.getname (), clazz);//gets the Property object to indicate which classMethod WM = Pd.getwritemethod ();//get write method, i.e. setterWm.invoke (obj, 2);//The wm method that executes the Obj object, 2 is the parameter. Because you know the attribute of type int, it is just a passing int. In fact, we need to judge the type of his parameter.      }      //Read Data       for(Field f:fields) {propertydescriptor pd=NewPropertyDescriptor (F.getname (), clazz);//gets the Property object to indicate which classMethod RM = Pd.getreadmethod ();//get Read method, getterInteger num = (integer) rm.invoke (obj);//the RM method that executes the Obj object. Because you know the attribute of type int, you convert it to an integer. You can also not convert direct printingSystem.out.println (num);}

The usage is still relatively simple.

The next step is how to call this method in spring to assign a value to Pojo.

The first doget to be traced back to the Org.springframework.web.servlet.FrameworkServlet class (HttpServletRequest request, HttpServletResponse Response) The Do method (do method is the basic method in the servlet, which is equivalent to spring taking over all do methods and then distributing the method. If you do not use the framework, you need to write your own servlet inheritance HttpServlet implementation of all do methods, but also can be used, but the server is large so it is too complex, the framework is for this existence)

This method calls ProcessRequest (request, response), ProcessRequest calls Doservice (request, response), Doservice is the core of all do methods, Be responsible for distributing the request to different service (@RequestMapping the method indicated by the annotation) according to the situation.

Doservice is implemented in Frameworkservlet sub-class Dispatcherservlet (Dispatch: Dispatch, Dispatch), Doservice is also called Dodispatch (Request, response), The appropriate method to assign the current request to the appropriate service.

Called in the Dodispatch Method (Handleradapter) Ha.handle (processedrequest, Response, Mappedhandler.gethandler ()) method to find the method that hooks the request (Processedrequest is converted by Checkmultipart (request)) (actually invoke the handler written in the note).

Then the handle method in Handleradapter

Spring Source Research: Data binding

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.