How does Spring 2.5 map the incoming request data to ModelAttribute command?
-
03-07-2019 - |
Question
I have used Spring 2.0 and now I am using Spring 2.5 Natually, Spring 2.5 made life very easy as far as the Web Controllers are concerned. The question keeps coming in my mind that in 2.0 we had to set the command class and accordingly the AbstractFormController used to populate that command object for us. In Spring 2.5 we don't do that so how it comes know that which class to use ? Does it depend on the type of parameter we have annotated @ModelAttribute("command") to ?
If yes, then please let me know any "Utils" class that provides the exact mechanism wherein I can pass HttpRequest and class name (or Class object) and that would return me the populated object !
Hope you got my question.
Thanks in advance.
Solution
The Web MVC annotations binding eventually rely upon an
org.springframework.beans.AbstractPropertyAccessor
which has two concrete implementations:
org.springframework.beans.BeanWrapperImpl org.springframework.beans.DirectFieldAccessor
Both accept a Java object, which will be the target to set the properties for.
The first, BeanWrapperImpl uses the setter/getter methods of a java object, while the other set the fields directly. Both checks that the methods/fields are public/accesible, and if otherwise, use reflection's setAccessible(true) to ensure it can be set.
From the Web MVC, an instance of AbstractPropertyAccessor is created, and the parameter map of a HttpRequest is passed to AbstractPropertyAccessor#setPropertyValues.
After the call, the java object is populated with whatever is in the map