user2740224
user2740224

Reputation:

Differencing between model.addAttribute("name",value) and mv.addObject("name",value)?

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

Answers (2)

Sotirios Delimanolis
Sotirios Delimanolis

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

Debojit Saikia
Debojit Saikia

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

Related Questions