I am new to Spring MVC and trying to learn the concepts from Spring MVC Docs:
A ModelAndView object, with the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
Can someone please help me in understanding this? What does it mean that "model implicitly enriched with command object" and also the "results of @ModelAttribute annotated reference data accessor methods"?
A Model object, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
How the view name is determined in Spring here?
A Map object for exposing a model, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
Same is the case here, how the view name is determined?
What does it mean that "model implicitly enriched with command object" and also the "results of @ModelAttribute annotated reference data accessor methods"?
Command objects are objects created by Spring from request parameters. In other words, say you had
public ModelAndView doSomething(SomeFoo foo, @ModelAttribute SomeBar bar) {
return new ModelAndView("view-name");
}
Spring would implicitly add the objects foo and bar are referencing to the ModelAndView returned. Note that, although SomeFoo is not annotated with @ModelAttribute, Spring will still consider it a model attribute. You'll have to look into HandlerMethodArgumentResolver to understand why.
You can also annotate methods with @ModelAttribute and those will be called and the object created added to the Model on each request handled by the @Controller your method is in. That is what is meant by results of @ModelAttribute annotated reference data accessor methods.
How the view name is determined in Spring here?
It tells you exactly how it is determined: it uses RequestToViewNameTranslator, where the default is DefaultRequestToViewNameTranslator. Read the javadoc for more details.
Same is the case here, how the view name is determined?
Same as above.
To answer the question in your comment, Spring registers a number of HandlerMethodArgumentResolver instances to resolve the arguments to pass to your @RequestMapping annotated method. It registers them in a specific order prioritizing parameters of specific types like Model, ModelMap, ModelAndView, etc. and then annotated parameters (eg. handlers for @PathVariable, @RequestParam, etc.). Spring then iterates through this list and uses the first one it finds that can handle the parameter.
One of these HandlerMethodArgumentResolver is ServletModelAttributeMethodProcessor which handles @ModelAttribute annotated parameters. Spring registers two (2) of these. One for parameters actually annotated with @ModelAttribute and one for parameters not annotated. This way you can inject your own HandlerMethodArgumentResolver to handle parameters your way.
Registration order of HandlerMethodArgumentResolver
...
ServletModelAttributeMethodProcessor for @ModelAttribute annotated parameters
...
your own HandlerMethodArgumentResolver
...
ServletModelAttributeMethodProcessor for parameters not annotated with ModelAttribute
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With