What are the benifits of using ModelMap instead of a simple Map in Spring MVC. I see in the code implementation that they put the datatype of the attribute added in the map as key instead to be made available on the form.
Can anyone explain with an example.
ModelMapsubclassesLinkedHashMap, and provides some additional conveniences to make it a bit easier to use by controllersaddAttributecan be called with just a value, and the map key is then inferred from the type.addAttributemethods all return theModelMap, so you can chain method called together, e.g.modelMap.addAttribute('x', x).addAttribute('y',y)addAttributemethods checks that the values aren’t nullModelMapis fixed atMap<String, Object>, which is the only one that makes sense for a view model.So nothing earth-shattering, but enough to make it a bit nicer than a raw
Map. Spring will let you use either one.You can also use the
Modelinterface, which provides nothing other than theaddAttributemethods, and is implemented by theExtendedModelMapclass which itself adds further conveniences.