Spring Framework

org.springframework.ui
Class ModelMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<java.lang.String,java.lang.Object>
              extended by org.springframework.ui.ModelMap
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.String,java.lang.Object>
Direct Known Subclasses:
ExtendedModelMap, RedirectAttributesModelMap

public class ModelMap
extends java.util.LinkedHashMap<java.lang.String,java.lang.Object>

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.

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
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
ModelMap()
          Construct a new, empty ModelMap.
ModelMap(java.lang.Object attributeValue)
          Construct a new ModelMap containing the supplied attribute.
ModelMap(java.lang.String attributeName, java.lang.Object attributeValue)
          Construct a new ModelMap containing the supplied attribute under the supplied name.
 
Method Summary
 ModelMap addAllAttributes(java.util.Collection<?> attributeValues)
          Copy all attributes in the supplied Collection into this Map, using attribute name generation for each element.
 ModelMap addAllAttributes(java.util.Map<java.lang.String,?> attributes)
          Copy all attributes in the supplied Map into this Map.
 ModelMap addAllObjects(java.util.Collection objects)
          Deprecated. as of Spring 2.5, in favor of addAllAttributes(Collection)
 ModelMap addAllObjects(java.util.Map objects)
          Deprecated. as of Spring 2.5, in favor of addAllAttributes(Map)
 ModelMap addAttribute(java.lang.Object attributeValue)
          Add the supplied attribute to this Map using a generated name.
 ModelMap addAttribute(java.lang.String attributeName, java.lang.Object attributeValue)
          Add the supplied attribute under the supplied name.
 ModelMap addObject(java.lang.Object modelObject)
          Deprecated. as of Spring 2.5, in favor of addAttribute(Object)
 ModelMap addObject(java.lang.String modelName, java.lang.Object modelObject)
          Deprecated. as of Spring 2.5, in favor of addAttribute(String, Object)
 boolean containsAttribute(java.lang.String attributeName)
          Does this model contain an attribute of the given name?
 ModelMap mergeAttributes(java.util.Map<java.lang.String,?> attributes)
          Copy all attributes in the supplied Map into this Map, with existing objects of the same name taking precedence (i.e.
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get, removeEldestEntry
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, put, putAll, remove, size, values
 
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
containsKey, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Constructor Detail

ModelMap

public ModelMap()
Construct a new, empty ModelMap.


ModelMap

public ModelMap(java.lang.String attributeName,
                java.lang.Object attributeValue)
Construct a new ModelMap containing the supplied attribute under the supplied name.

See Also:
addAttribute(String, Object)

ModelMap

public ModelMap(java.lang.Object attributeValue)
Construct a new ModelMap containing 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(java.lang.String attributeName,
                             java.lang.Object attributeValue)
Add the supplied attribute under the supplied name.

Parameters:
attributeName - the name of the model attribute (never null)
attributeValue - the model attribute value (can be null)

addAttribute

public ModelMap addAttribute(java.lang.Object attributeValue)
Add the supplied attribute to this Map using a generated name.

Note: Empty Collections are not added to the model when using this method because we cannot correctly determine the true convention name. View code should check for null rather than for empty collections as is already done by JSTL tags.

Parameters:
attributeValue - the model attribute value (never null)

addAllAttributes

public ModelMap addAllAttributes(java.util.Collection<?> attributeValues)
Copy all attributes in the supplied Collection into this Map, using attribute name generation for each element.

See Also:
addAttribute(Object)

addAllAttributes

public ModelMap addAllAttributes(java.util.Map<java.lang.String,?> attributes)
Copy all attributes in the supplied Map into this Map.

See Also:
addAttribute(String, Object)

mergeAttributes

public ModelMap mergeAttributes(java.util.Map<java.lang.String,?> attributes)
Copy all attributes in the supplied Map into this Map, with existing objects of the same name taking precedence (i.e. not getting replaced).


containsAttribute

public boolean containsAttribute(java.lang.String attributeName)
Does this model contain an attribute of the given name?

Parameters:
attributeName - the name of the model attribute (never null)
Returns:
whether this model contains a corresponding attribute

addObject

@Deprecated
public ModelMap addObject(java.lang.String modelName,
                                     java.lang.Object modelObject)
Deprecated. as of Spring 2.5, in favor of addAttribute(String, Object)


addObject

@Deprecated
public ModelMap addObject(java.lang.Object modelObject)
Deprecated. as of Spring 2.5, in favor of addAttribute(Object)


addAllObjects

@Deprecated
public ModelMap addAllObjects(java.util.Collection objects)
Deprecated. as of Spring 2.5, in favor of addAllAttributes(Collection)


addAllObjects

@Deprecated
public ModelMap addAllObjects(java.util.Map objects)
Deprecated. as of Spring 2.5, in favor of addAllAttributes(Map)


Spring Framework