Differences between Model, ModelMap and ModelAndView
Model: This is the interface. It defines a holder for model attributes and is primarily intended to add attributes to a model.
Example:
@RequestMapping(method = RequestMethod.GET) public String printHello(Model model) { model.addAttribute("message", "Hello World!!"); return "hello"; }
ModelMap: Implement a map to use when creating model data for use with user interface tools. Supports chained calls and generates model attribute names.
Example:
@RequestMapping("/helloworld") public String hello(ModelMap map) { String helloWorldMessage = "Hello world!"; String welcomeMessage = "Welcome!"; map.addAttribute("helloMessage", helloWorldMessage); map.addAttribute("welcomeMessage", welcomeMessage); return "hello"; }
ModelAndView: This class simply contains both to allow the controller to return both models and the view to a single return value.
Example:
@RequestMapping("/welcome") public ModelAndView helloWorld() { String message = "Hello World!"; return new ModelAndView("welcome", "message", message); }
vikas harle Jun 30 '17 at 6:38 2017-06-30 06:38
source share