What is the difference between declaring @ModelAttribute
as a method parameter and using model.asMap()
?
Example:
public void method1(@ModelAttribute("attr") MyObject myObj) {
...
}
public void method2(Model model) {
MyObject myObj = (MyObject) model.asMap().get("attr");
}
These are not the same if that's what your thinking. I tried to get an attribute from the model doing it the first way but didn't work so ended up doing it the second way.
Thanks
EDIT: What I mean by "it didn't work" is that when I used @ModelAttribute
, it was confusing the values of the variables in that object to another object of a different type which had the same variable names.
EDIT #2: No it is not a duplicate because he does not address the model.asMap() method. As per the other post, I tried using @ModelAttribute on a @SessionAttributes variable but ran into problems so had to use model.asMap()