11

I am comparing ModelMap and Model from Spring MVC. Apart from obvious difference that first is a class and second an interface is there any difference that make a usage of one or another preferable in different situations?

Primarily they are used for same purpose (as from spring MVC documentation):

java.util.Map / org.springframework.ui.Model / org.springframework.ui.ModelMap for enriching the implicit model that is exposed to the web view.

Only I have found out there it that ModelMap is enhanced Map, however difference to Model is still unclear.

Can I conclude from ModelMap javadoc that 'Model' is preferred for java 5?

Check out the Model interface for a Java-5-based interface variant that serves the same purpose.

However it does not seem that ModelMap would be deprecated or something. But why does ModelMap does not implement Model?

sodik
  • 4,675
  • 2
  • 29
  • 47
  • 2
    `ExtendedModelMap` is extending `ModelMap` and implementing `ModelMap`. When using the `Model` you can chain method calls as most methods return `Model` with a `Map` or `ModelMap` this isn't the case. – M. Deinum Jan 28 '15 at 15:22
  • thanks for `ExtendedModelMap`. AFAIK you can chain calls with `ModelMap` as well (in difference to `Map` that I have already noticed). – sodik Jan 28 '15 at 15:35
  • Ah yes you are right. `ModelMap` also predates the `Model` interface. For that interface there are basically 2 implementations, the `ExtendedModelMap` and the `RedirectAttributesModelMap` (there is also the `BindingAwareModelMap` which is an extension the the `ExtendedModelMap`). For the usage in the request mapping methods it doesn't really matter which one to use, although I would probably suggest to use the `Model` interface instead one of the implementations. But that is highly opinionated :). – M. Deinum Jan 28 '15 at 17:42
  • answer for a similar question can be found at https://stackoverflow.com/questions/18486660/what-are-the-differences-between-model-modelmap-and-modelandview – Laky Feb 08 '18 at 08:00

1 Answers1

-1

Model - Java-5-specific interface that defines a holder for model attributes. Primarily designed for adding attributes to the model. Allows for accessing the overall model as a java.util.Map.

Simply put, the model can supply attributes used for rendering views.


ModelMap - ModelMap class is basically a LinkedHashMap. It add some methods for convenience. Just like the Model interface above, ModelMap is also used to pass values to render a view.

The advantage of ModelMap is it gives us the ability to pass a collection of values and treat these values as if they were within a Map:

@GetMapping("/printViewPage")
public String passParametersWithModelMap(ModelMap map) {
    map.addAttribute("welcomeMessage", "welcome");
    map.addAttribute("message", "Baeldung");
    return "viewPage";
}

reference;

Laky
  • 632
  • 3
  • 10
  • 5
    Hi, The sample code provided does not seem to demonstrate the ability "to pass a collection of values" . When i look at the API for Model (not ModelMap), i notice that Model also provide api called add addAllAttributes, which also allow "to pass a collection of values". So ultimately, I still don't see the difference between modelMap and Model. – Nick Wills Jan 06 '20 at 05:19