Reputation:
What differencing between
model.addAttribute("name",value)
and
mv.addObject("name",value)
in spring-mvc?
model is Model
mv is ModelAndView
Upvotes: 6
Views: 8888
Reputation: 280072
Model#addAttribute(String, Object)
states
Add the supplied attribute under the supplied name.
while ModelAndView#addObject(String, Object)
states
Add an attribute to the model.
If you look at the source code for addObject
public ModelAndView addObject(String attributeName, Object attributeValue) {
getModelMap().addAttribute(attributeName, attributeValue);
return this;
}
it's delegating to the Model
reference that a ModelAndView
holds and calling addAttribute()
on it.
Upvotes: 6
Reputation: 10632
Model is a holder for model attributes only.
ModelAndView is a holder for both Model and View , so that the controller can return both model and view together.
Upvotes: 3