Package org.springframework.ui
Class ModelMap
- java.lang.Object
- java.util.AbstractMap<K,V>
- java.util.HashMap<K,V>
- java.util.LinkedHashMap<String,Object>
- org.springframework.ui.ModelMap
- All Implemented Interfaces:
Serializable,Cloneable,Map<String,Object>
- Direct Known Subclasses:
ExtendedModelMap,RedirectAttributesModelMap
public class ModelMap extends LinkedHashMap<String,Object>
Implementation ofMapfor 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
Modelinterface for a Java-5-based interface variant that serves the same purpose.- Since:
- 2.0
- Author:
- Rob Harrop, Juergen Hoeller
- See Also:
Conventions.getVariableName(java.lang.Object),ModelAndView,ModelAndView, Serialized Form
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
Constructor Summary
Constructors Constructor Description ModelMap()Construct a new, emptyModelMap.ModelMap(Object attributeValue)Construct a newModelMapcontaining the supplied attribute.ModelMap(String attributeName, Object attributeValue)Construct a newModelMapcontaining the supplied attribute under the supplied name.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ModelMapaddAllAttributes(Collection<?> attributeValues)Copy all attributes in the suppliedCollectioninto thisMap, using attribute name generation for each element.ModelMapaddAllAttributes(Map<String,?> attributes)Copy all attributes in the suppliedMapinto thisMap.ModelMapaddAttribute(Object attributeValue)Add the supplied attribute to thisMapusing agenerated name.ModelMapaddAttribute(String attributeName, Object attributeValue)Add the supplied attribute under the supplied name.booleancontainsAttribute(String attributeName)Does this model contain an attribute of the given name?ModelMapmergeAttributes(Map<String,?> attributes)Copy all attributes in the suppliedMapinto thisMap, with existing objects of the same name taking precedence (i.e.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
Constructor Detail
ModelMap
public ModelMap()
Construct a new, emptyModelMap.
ModelMap
public ModelMap(String attributeName, Object attributeValue)
Construct a newModelMapcontaining the supplied attribute under the supplied name.- See Also:
addAttribute(String, Object)
ModelMap
public ModelMap(Object attributeValue)
Construct a newModelMapcontaining the supplied attribute. Uses attribute name generation to generate the key for the supplied model object.- See Also:
addAttribute(Object)
Method Detail
addAttribute
public ModelMap addAttribute(String attributeName, Object attributeValue)
Add the supplied attribute under the supplied name.- Parameters:
attributeName- the name of the model attribute (nevernull)attributeValue- the model attribute value (can benull)
addAttribute
public ModelMap addAttribute(Object attributeValue)
Add the supplied attribute to thisMapusing agenerated name.Note: Empty Collectionsare not added to the model when using this method because we cannot correctly determine the true convention name. View code should check fornullrather than for empty collections as is already done by JSTL tags.- Parameters:
attributeValue- the model attribute value (nevernull)
addAllAttributes
public ModelMap addAllAttributes(Collection<?> attributeValues)
Copy all attributes in the suppliedCollectioninto thisMap, using attribute name generation for each element.- See Also:
addAttribute(Object)
addAllAttributes
public ModelMap addAllAttributes(Map<String,?> attributes)
Copy all attributes in the suppliedMapinto thisMap.- See Also:
addAttribute(String, Object)
mergeAttributes
public ModelMap mergeAttributes(Map<String,?> attributes)
Copy all attributes in the suppliedMapinto thisMap, with existing objects of the same name taking precedence (i.e. not getting replaced).
containsAttribute
public boolean containsAttribute(String attributeName)
Does this model contain an attribute of the given name?- Parameters:
attributeName- the name of the model attribute (nevernull)- Returns:
- whether this model contains a corresponding attribute