Reputation: 435
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.
Upvotes: 29
Views: 41527
Reputation:
ModalMap extends LinkedHashMap
Implementation of Map for use when building model data for use with UI tools. Supports chained calls and generation of model attribute names.
This class serves as generic model holder for both Servlet and Portlet MVC, but is not tied to either of those. Check out the Model interface for a Java-5-based interface variant that serves the same purpose.
Upvotes: 0
Reputation: 101
ModelMap.addAttribute
will do NULL check, ModelMap.put
is inherit from LinkedHashMap
Upvotes: 2
Reputation: 403461
ModelMap
subclasses LinkedHashMap
, and provides some additional conveniences to make it a bit easier to use by controllers
addAttribute
can be called with just a value, and the map key is then inferred from the type. addAttribute
methods all return the ModelMap
, so you can chain method called together, e.g. modelMap.addAttribute('x', x).addAttribute('y',y)
addAttribute
methods checks that the values aren't nullModelMap
is fixed at Map<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 Model
interface, which provides nothing other than the addAttribute
methods, and is implemented by the ExtendedModelMap
class which itself adds further conveniences.
Upvotes: 47