82

What are the main differences between the following Spring Framework classes?

  • Model
  • ModelMap
  • ModelAndView

Using Model.put(String,Object) we can access the values in .jsp files, but ModelMap.addAttribute(String,Object) also did same thing. I do not understand the difference between these classes.

Evan Wondrasek
  • 2,610
  • 3
  • 22
  • 21
Basavaraj
  • 1,081
  • 1
  • 11
  • 21

3 Answers3

81

Model is an interface while ModelMap is a class.

ModelAndView is just a container for both a ModelMap and a view object. It allows a controller to return both as a single value.

Bart
  • 17,070
  • 5
  • 61
  • 80
  • 5
    After digging around, the best reason that I found to use a ModelMap (kind of buried in vikas harle's answer and not shown in example) is that when adding attributes to a ModelMap, you can leave out the attribute name (key) and spring will generate the attribute key based on the attribute value. https://docs.spring.io/spring/docs/4.3.14.RELEASE/spring-framework-reference/htmlsingle/#mvc-coc-modelmap – Tim Jan 29 '18 at 23:02
  • So, then what's the point and different when returning ModelAndView? in the case of returning String and letting ViewResolver to fetch respective view page, you still return only one type - and it's string (view name), model is being implicitly taken cared by DispatcherServlet.. what's the use-case distinction? – Giorgi Tsiklauri Apr 22 '19 at 11:14
36

Differences between Model, ModelMap, and ModelAndView

Model: It is an Interface. It defines a holder for model attributes and primarily designed for adding attributes to the model.

Example:

@RequestMapping(method = RequestMethod.GET)
    public String printHello(Model model) {
          model.addAttribute("message", "Hello World!!");
          return "hello";
       }

ModelMap: Implementation of Map for use when building model data for use with UI tools.Supports chained calls and generation of 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 merely holds both to make it possible for a controller to return both model and view in a single return value.

Example:

@RequestMapping("/welcome")
public ModelAndView helloWorld() {
        String message = "Hello World!";
        return new ModelAndView("welcome", "message", message);
    }
Vikas Harle
  • 377
  • 3
  • 3
  • Should we add Model or ModelMap in the method parameters to access it in the view. – omkar sirra Oct 11 '17 at 06:23
  • 4
    Your second example doesn't show the difference between `Model` and `ModelMap`... both examples are the same. – Jan Feb 24 '21 at 15:41
  • Model also support chained calls. Model is an interface, whereas ModelMap is an implementation of Map interface. Hence the latter have method such as forEach, which the former don't have – Nick Wills Oct 10 '22 at 04:44
14

Model: is an interface it contains four addAttribute and one merAttribute method.

ModelMap: implements Map interface. It also contains Map method.

ModelAndView: As Bart explain it allows a controller return both as a single value.

Ashu
  • 479
  • 6
  • 6
  • Is "merAttribute" actually referring to "mergeAttributes"? Also, there are not "four addAttribute" methods. Rather, there are four methods for adding attributes, at least as far as version 5.0.4 of the Spring framework is concerned. I'm also confused by the "Map method" statement in the second line. Does that refer to the inherited methods from java.util.Map, or something else? – GoldDragonTSU Mar 22 '18 at 20:18