ModelMap vs Model in Spring MVC

I am comparing ModelMap and Model from Spring MVC. Besides the obvious difference in the first class and in the second interface , is there any difference that makes the use of one or the other preferable in different situations?

They are primarily used for the same purpose (for example, from spring MVC ):

java.util.Map/org.springframework.ui.Model/ org.springframework.ui.ModelMap to enrich the implicit model that is displayed on the web page.

Only I found there that ModelMap improved by Map , however the difference before Model is still unclear.

Can I conclude from ModelMap javadoc that "Model" is preferable for java 5?

Check out the model interface for a Java-5-based interface option that serves the same purpose.

However, it does not seem that ModelMap will be obsolete or anything else. But why ModelMap n't ModelMap implement Model ?

+6
source share
1 answer

Model is a Java-5 specific interface that defines a holder for model attributes. Primarily designed to add attributes to the model. Allows access to the general model as java.util.Map.

Simply put, a model can provide attributes used to render views.


ModelMap - The ModelMap class is mainly associated with LinkedHashMap. He adds some methods for convenience. Like the Model above, ModelMap is also used to pass values ​​to render a view.

The advantage of ModelMap is that it allows us to transfer a set of values ​​and process these values ​​as if they were within the Map:

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

link

0
source

Source: https://habr.com/ru/post/981677/


All Articles