Reputation: 103
Currently I am learning Spring specifically the Spring MVC part and I stumbled on thing I don't quite understand how does it work.
@RequestMapping("/foo")
public String foo(Model model){
// code here
return "foo";
}
The method above annotated with @RequestMapping
receive the parameter with class Model
and sometimes ModelAndView
.
What I don't understand is where the Model
parameter comes from and how the flow is from the spring configuration class (Such as WebConfig.java) which use the ComponentScan
and a Bean of InternalResourceViewResolver
.
I've been looking for sources and I don't find anything helpful for me, even the documentation, which makes me to ask here for the first time.
A direct explanation will be really helpful, or if there are any sources can be put the link here. If it is from the documentation please mention which part/section it is because I might miss one or two thing.
Upvotes: 1
Views: 4118
Reputation: 5466
The method above annotated with @RequestMapping receive the parameter with class Model and sometimes ModelAndView.
The below post explains in detail about ModelAndView and Model
When to use ModelAndView vs Model in Spring?
where the Model parameter comes from
A Controller is typically responsible for preparing a model Map with data and selecting a view name but it can also write directly to the response stream and complete the request. View name resolution is highly configurable through file extension or Accept header content type negotiation, through bean names, a properties file, or even a custom ViewResolver implementation. The model (the M in MVC) is a Map interface, which allows for the complete abstraction of the view technology. You can integrate directly with template based rendering technologies such as JSP, Velocity and Freemarker, or directly generate XML, JSON, Atom, and many other types of content. The model Map is simply transformed into an appropriate format, such as JSP request attributes, a Velocity template model.
Reference - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-arguments
Upvotes: 4