ModelMap vs Model in Spring MVC ModelMap vs Model in Spring MVC spring spring

ModelMap vs Model in Spring MVC


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;