Top Description Constructors Methods
org.springframework.ui

public Class ModelMap

extends LinkedHashMap<String, Object>
Class Inheritance
Known Direct Subclasses
org.springframework.ui.ExtendedModelMap
Annotations
@SuppressWarnings:serial
Imports
java.util.Collection, .LinkedHashMap, .Map, org.springframework.core.Conventions, org.springframework.lang.Nullable, org.springframework.util.Assert

Implementation of java.util.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 Servlet MVC but is not tied to it. Check out the Model interface for an interface variant.

Authors
Rob Harrop, Juergen Hoeller
Since
2.0
See Also
Conventions#getVariableName, org.springframework.web.servlet.ModelAndView

Constructor Summary

AccessConstructor and Description
public
ModelMap()

Construct a new, empty ModelMap.

public
ModelMap(String attributeName, Object attributeValue)

Construct a new ModelMap containing the supplied attribute under the supplied name.

public
ModelMap(Object attributeValue)

Construct a new ModelMap containing the supplied attribute.

Method Summary

Modifier and TypeMethod and Description
public ModelMap
addAllAttributes(Collection<?> attributeValues)

Copy all attributes in the supplied Collection into this Map, using attribute name generation for each element.

public ModelMap
addAllAttributes(Map<String, ?> attributes)

Copy all attributes in the supplied Map into this Map.

public ModelMap
addAttribute(String
the name of the model attribute (never null)
attributeName
,
Object
the model attribute value (can be null)
attributeValue
)

Add the supplied attribute under the supplied name.

public ModelMap
addAttribute(Object
the model attribute value (never null)
attributeValue
)

Add the supplied attribute to this Map using a generated name.

public boolean

Returns:

whether this model contains a corresponding attribute
containsAttribute
(String
the name of the model attribute (never null)
attributeName
)

Does this model contain an attribute of the given name?

public Object

Returns:

the corresponding attribute value, or null if none
getAttribute
(String
the name of the model attribute (never null)
attributeName
)

Return the attribute value for the given name, if any.

public ModelMap
mergeAttributes(Map<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).

Inherited from java.util.LinkedHashMap:
clearcontainsValueentrySetforEachgetgetOrDefaultkeySetnewLinkedHashMapputFirstputLastremoveEldestEntryreplaceAllreversedsequencedEntrySetsequencedKeySetsequencedValuesvalues

Constructor Detail

ModelMapback to summary
public ModelMap()

Construct a new, empty ModelMap.

ModelMapback to summary
public ModelMap(String attributeName, Object attributeValue)

Construct a new ModelMap containing the supplied attribute under the supplied name.

See Also
addAttribute(String, Object)
ModelMapback to summary
public ModelMap(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

addAllAttributesback to summary
public ModelMap addAllAttributes(Collection<?> attributeValues)

Copy all attributes in the supplied Collection into this Map, using attribute name generation for each element.

See Also
addAttribute(Object)
addAllAttributesback to summary
public ModelMap addAllAttributes(Map<String, ?> attributes)

Copy all attributes in the supplied Map into this Map.

See Also
addAttribute(String, Object)
addAttributeback to summary
public ModelMap addAttribute(String attributeName, Object attributeValue)

Add the supplied attribute under the supplied name.

Parameters
attributeName:String

the name of the model attribute (never null)

attributeValue:Object

the model attribute value (can be null)

addAttributeback to summary
public ModelMap addAttribute(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:Object

the model attribute value (never null)

containsAttributeback to summary
public boolean containsAttribute(String attributeName)

Does this model contain an attribute of the given name?

Parameters
attributeName:String

the name of the model attribute (never null)

Returns:boolean

whether this model contains a corresponding attribute

getAttributeback to summary
public Object getAttribute(String attributeName)

Return the attribute value for the given name, if any.

Parameters
attributeName:String

the name of the model attribute (never null)

Returns:Object

the corresponding attribute value, or null if none

Annotations
@Nullable
Since
5.2
mergeAttributesback to summary
public ModelMap mergeAttributes(Map<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).