Model, ModelMap and ModelAndView
Model
Model is an Interface
Model is used to pass value to render view.
Model interface allows you to add the attributes in the object and those attributes will be copied in the request attributes and that can be used in the view.
@RequestMapping("/model")
public String addParametersInModel(Model model) {
User userData = new User();
model.addAttribue(userData); // key name will be 'user', with respective to the class name if no key is provided.
return "viewPage";
}
ModelMap
ModelMap is a class.
It extends LinkedHashMap.
It has multiple constructors and method implementations.
ModelMap is also used to pass values to render view.
@RequestMapping("/getModel")
public String addParametersInModel(ModelMap model) {
User userData = new User();
model.addAttribue(userData); // key name will be 'user', with respective to the class name if no key is provided.
return "viewPage";
}
ModelAndView
It is a Holder for both View and Model in the Spring MVC framework.
It make possible for controller to return view and Model in a single return object.
@RequestMapping("/view")
public ModelAndView show() {
ModelAndView modelAndView = new ModelAndView("viewPage");
modelAndView.addObject("message", "Good Morning");
return modelAndView;
}