Top Description Fields Constructors Methods
org.springframework.web.servlet

public Class ModelAndView

extends Object
Class Inheritance
Imports
java.util.Map, org.springframework.http.HttpStatusCode, org.springframework.lang.Nullable, org.springframework.ui.ModelMap, org.springframework.util.CollectionUtils

Holder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value.

Represents a model and view returned by a handler, to be resolved by a DispatcherServlet. The view can take the form of a String view name which will need to be resolved by a ViewResolver object; alternatively a View object can be specified directly. The model is a Map, allowing the use of multiple objects keyed by name.

Authors
Rod Johnson, Juergen Hoeller, Rob Harrop, Rossen Stoyanchev
See Also
DispatcherServlet, ViewResolver, HandlerAdapter#handle, org.springframework.web.servlet.mvc.Controller#handleRequest

Field Summary

Modifier and TypeField and Description
private boolean
cleared

Indicates whether this instance has been cleared with a call to clear().

private ModelMap
model

Model Map.

private HttpStatusCode
status

Optional HTTP status for the response.

private Object
view

View instance or view name String.

Constructor Summary

AccessConstructor and Description
public
ModelAndView()

Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.

public
ModelAndView(String
name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
viewName
)

Convenient constructor when there is no model data to expose.

public
ModelAndView(View
the View object to render
view
)

Convenient constructor when there is no model data to expose.

public
ModelAndView(String
name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
viewName
,
Map<String, ?>
a Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.
model
)

Create a new ModelAndView given a view name and a model.

public
ModelAndView(View
the View object to render
view
,
Map<String, ?>
a Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.
model
)

Create a new ModelAndView given a View object and a model.

public
ModelAndView(String
name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
viewName
,
HttpStatusCode
an HTTP status code to use for the response (to be set just prior to View rendering)
status
)

Create a new ModelAndView given a view name and HTTP status.

public
ModelAndView(String
name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
viewName
,
Map<String, ?>
a Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.
model
,
HttpStatusCode
an HTTP status code to use for the response (to be set just prior to View rendering)
status
)

Create a new ModelAndView given a view name, model, and HTTP status.

public
ModelAndView(String
name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
viewName
,
String
name of the single entry in the model
modelName
,
Object
the single model object
modelObject
)

Convenient constructor to take a single model object.

public
ModelAndView(View
the View object to render
view
,
String
name of the single entry in the model
modelName
,
Object
the single model object
modelObject
)

Convenient constructor to take a single model object.

Method Summary

Modifier and TypeMethod and Description
public ModelAndView
addAllObjects(Map<String, ?>
a Map of attributeName → attributeValue pairs
modelMap
)

Add all attributes contained in the provided Map to the model.

public ModelAndView
addObject(String
name of the object to add to the model (never null)
attributeName
,
Object
object to add to the model (can be null)
attributeValue
)

Add an attribute to the model.

public ModelAndView
addObject(Object
the object to add to the model (never null)
attributeValue
)

Add an attribute to the model using parameter name generation.

public void
clear()

Clear the state of this ModelAndView object.

private String
public Map<String, Object>
getModel()

Return the model map.

protected Map<String, Object>
getModelInternal()

Return the model map.

public ModelMap
getModelMap()

Return the underlying ModelMap instance (never null).

public HttpStatusCode
getStatus()

Return the configured HTTP status for the response, if any.

public View
getView()

Return the View object, or null if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.

public String
getViewName()

Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.

public boolean
hasView()

Indicate whether this ModelAndView has a view, either as a view name or as a direct View instance.

public boolean
isEmpty()

Return whether this ModelAndView object is empty, i.e. whether it does not hold any view and does not contain a model.

public boolean
isReference()

Return whether we use a view reference, i.e. true if the view has been specified via a name to be resolved by the DispatcherServlet via a ViewResolver.

public void
setStatus(HttpStatusCode status)

Set the HTTP status to use for the response.

public void
setView(View view)

Set a View object for this ModelAndView.

public void
setViewName(String viewName)

Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver.

public String
toString()

Overrides java.lang.Object.toString.

Return diagnostic information about this model and view.

public boolean
wasCleared()

Return whether this ModelAndView object is empty as a result of a call to clear i.e. whether it does not hold any view and does not contain a model.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

clearedback to summary
private boolean cleared

Indicates whether this instance has been cleared with a call to clear().

modelback to summary
private ModelMap model

Model Map.

Annotations
@Nullable
statusback to summary
private HttpStatusCode status

Optional HTTP status for the response.

Annotations
@Nullable
viewback to summary
private Object view

View instance or view name String.

Annotations
@Nullable

Constructor Detail

ModelAndViewback to summary
public ModelAndView()

Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.

See Also
setView(View), setViewName(String)
ModelAndViewback to summary
public ModelAndView(String viewName)

Convenient constructor when there is no model data to expose. Can also be used in conjunction with addObject.

Parameters
viewName:String

name of the View to render, to be resolved by the DispatcherServlet's ViewResolver

See Also
addObject
ModelAndViewback to summary
public ModelAndView(View view)

Convenient constructor when there is no model data to expose. Can also be used in conjunction with addObject.

Parameters
view:View

the View object to render

See Also
addObject
ModelAndViewback to summary
public ModelAndView(String viewName, Map<String, ?> model)

Create a new ModelAndView given a view name and a model.

Parameters
viewName:String

name of the View to render, to be resolved by the DispatcherServlet's ViewResolver

model:Map<String, ?>

a Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.

ModelAndViewback to summary
public ModelAndView(View view, Map<String, ?> model)

Create a new ModelAndView given a View object and a model.

Note

the supplied model data is copied into the internal storage of this class. You should not consider to modify the supplied Map after supplying it to this class

Parameters
view:View

the View object to render

model:Map<String, ?>

a Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.

ModelAndViewback to summary
public ModelAndView(String viewName, HttpStatusCode status)

Create a new ModelAndView given a view name and HTTP status.

Parameters
viewName:String

name of the View to render, to be resolved by the DispatcherServlet's ViewResolver

status:HttpStatusCode

an HTTP status code to use for the response (to be set just prior to View rendering)

Since
4.3.8
ModelAndViewback to summary
public ModelAndView(String viewName, Map<String, ?> model, HttpStatusCode status)

Create a new ModelAndView given a view name, model, and HTTP status.

Parameters
viewName:String

name of the View to render, to be resolved by the DispatcherServlet's ViewResolver

model:Map<String, ?>

a Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.

status:HttpStatusCode

an HTTP status code to use for the response (to be set just prior to View rendering)

Since
4.3
ModelAndViewback to summary
public ModelAndView(String viewName, String modelName, Object modelObject)

Convenient constructor to take a single model object.

Parameters
viewName:String

name of the View to render, to be resolved by the DispatcherServlet's ViewResolver

modelName:String

name of the single entry in the model

modelObject:Object

the single model object

ModelAndViewback to summary
public ModelAndView(View view, String modelName, Object modelObject)

Convenient constructor to take a single model object.

Parameters
view:View

the View object to render

modelName:String

name of the single entry in the model

modelObject:Object

the single model object

Method Detail

addAllObjectsback to summary
public ModelAndView addAllObjects(Map<String, ?> modelMap)

Add all attributes contained in the provided Map to the model.

Parameters
modelMap:Map<String, ?>

a Map of attributeName → attributeValue pairs

See Also
ModelMap#addAllAttributes(Map), getModelMap()
addObjectback to summary
public ModelAndView addObject(String attributeName, Object attributeValue)

Add an attribute to the model.

Parameters
attributeName:String

name of the object to add to the model (never null)

attributeValue:Object

object to add to the model (can be null)

See Also
ModelMap#addAttribute(String, Object), getModelMap()
addObjectback to summary
public ModelAndView addObject(Object attributeValue)

Add an attribute to the model using parameter name generation.

Parameters
attributeValue:Object

the object to add to the model (never null)

See Also
ModelMap#addAttribute(Object), getModelMap()
clearback to summary
public void clear()

Clear the state of this ModelAndView object. The object will be empty afterwards.

Can be used to suppress rendering of a given ModelAndView object in the postHandle method of a HandlerInterceptor.

See Also
isEmpty(), HandlerInterceptor#postHandle
formatViewback to summary
private String formatView()
getModelback to summary
public Map<String, Object> getModel()

Return the model map. Never returns null. To be called by application code for modifying the model.

getModelInternalback to summary
protected Map<String, Object> getModelInternal()

Return the model map. May return null. Called by DispatcherServlet for evaluation of the model.

Annotations
@Nullable
getModelMapback to summary
public ModelMap getModelMap()

Return the underlying ModelMap instance (never null).

getStatusback to summary
public HttpStatusCode getStatus()

Return the configured HTTP status for the response, if any.

Annotations
@Nullable
Since
4.3
getViewback to summary
public View getView()

Return the View object, or null if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.

Annotations
@Nullable
getViewNameback to summary
public String getViewName()

Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.

Annotations
@Nullable
hasViewback to summary
public boolean hasView()

Indicate whether this ModelAndView has a view, either as a view name or as a direct View instance.

isEmptyback to summary
public boolean isEmpty()

Return whether this ModelAndView object is empty, i.e. whether it does not hold any view and does not contain a model.

isReferenceback to summary
public boolean isReference()

Return whether we use a view reference, i.e. true if the view has been specified via a name to be resolved by the DispatcherServlet via a ViewResolver.

setStatusback to summary
public void setStatus(HttpStatusCode status)

Set the HTTP status to use for the response.

The response status is set just prior to View rendering.

Since
4.3
setViewback to summary
public void setView(View view)

Set a View object for this ModelAndView. Will override any pre-existing view name or View.

setViewNameback to summary
public void setViewName(String viewName)

Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver. Will override any pre-existing view name or View.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Return diagnostic information about this model and view.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
wasClearedback to summary
public boolean wasCleared()

Return whether this ModelAndView object is empty as a result of a call to clear i.e. whether it does not hold any view and does not contain a model.

Returns false if any additional state was added to the instance after the call to clear.

See Also
clear()