简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM