public class

ModelAndView

extends Object
java.lang.Object
   ↳ org.springframework.web.portlet.ModelAndView

Class Overview

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 DispatcherPortlet. 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.

Summary

Public Constructors
ModelAndView()
Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.
ModelAndView(String viewName)
Convenient constructor when there is no model data to expose.
ModelAndView(Object view)
Convenient constructor when there is no model data to expose.
ModelAndView(String viewName, Map<String, ?> model)
Create a new ModelAndView given a view name and a model.
ModelAndView(Object view, Map<String, ?> model)
Create a new ModelAndView given a View object and a model.
ModelAndView(String viewName, String modelName, Object modelObject)
Convenient constructor to take a single model object.
ModelAndView(Object view, String modelName, Object modelObject)
Convenient constructor to take a single model object.
Public Methods
ModelAndView addAllObjects(Map<String, ?> modelMap)
Add all attributes contained in the provided Map to the model.
ModelAndView addObject(String attributeName, Object attributeValue)
Add an attribute to the model.
ModelAndView addObject(Object attributeValue)
Add an attribute to the model using parameter name generation.
void clear()
Clear the state of this ModelAndView object.
Map<StringObject> getModel()
Return the model map.
ModelMap getModelMap()
Return the underlying ModelMap instance (never null).
Object getView()
Return the View object, or null if we are using a view name to be resolved by the DispatcherPortlet via a ViewResolver.
String getViewName()
Return the view name to be resolved by the DispatcherPortlet via a ViewResolver, or null if we are using a view object.
boolean hasView()
Indicate whether or not this ModelAndView has a view, either as a view name or as a direct view instance.
boolean isEmpty()
Return whether this ModelAndView object is empty, i.e.
boolean isReference()
Return whether we use a view reference, i.e.
void setView(Object view)
Set a View object for this ModelAndView.
void setViewName(String viewName)
Set a view name for this ModelAndView, to be resolved by the DispatcherPortlet via a ViewResolver.
String toString()
Return diagnostic information about this model and view.
boolean wasCleared()
Return whether this ModelAndView object is empty as a result of a call to clear() i.e.
Protected Methods
Map<StringObject> getModelInternal()
Return the model map.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public ModelAndView ()

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

public ModelAndView (String viewName)

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

Parameters
viewName name of the View to render, to be resolved by the DispatcherPortlet's ViewResolver

public ModelAndView (Object view)

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

Parameters
view View object to render (usually a Servlet MVC View object)

public ModelAndView (String viewName, Map<String, ?> model)

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

Parameters
viewName name of the View to render, to be resolved by the DispatcherPortlet's ViewResolver
model 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.

public ModelAndView (Object view, Map<String, ?> model)

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

Parameters
view View object to render (usually a Servlet MVC View object)
model 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.

public ModelAndView (String viewName, String modelName, Object modelObject)

Convenient constructor to take a single model object.

Parameters
viewName name of the View to render, to be resolved by the DispatcherPortlet's ViewResolver
modelName name of the single entry in the model
modelObject the single model object

public ModelAndView (Object view, String modelName, Object modelObject)

Convenient constructor to take a single model object.

Parameters
view View object to render (usually a Servlet MVC View object)
modelName name of the single entry in the model
modelObject the single model object

Public Methods

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

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

Parameters
modelMap a Map of attributeName -> attributeValue pairs

public ModelAndView addObject (String attributeName, Object attributeValue)

Add an attribute to the model.

Parameters
attributeName name of the object to add to the model
attributeValue object to add to the model (never null)

public ModelAndView addObject (Object attributeValue)

Add an attribute to the model using parameter name generation.

Parameters
attributeValue the object to add to the model (never null)

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 postHandleRender method of a HandlerInterceptor.

public Map<StringObject> getModel ()

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

public ModelMap getModelMap ()

Return the underlying ModelMap instance (never null).

public Object getView ()

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

public String getViewName ()

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

public boolean hasView ()

Indicate whether or not 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 DispatcherPortlet via a ViewResolver.

public void setView (Object view)

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

The given View object will usually be a Servlet MVC View object. This is nevertheless typed as Object to avoid a Servlet API dependency in the Portlet ModelAndView class.

public void setViewName (String viewName)

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

public String 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. Returns false if any additional state was added to the instance after the call to clear().

See Also

Protected Methods

protected Map<StringObject> getModelInternal ()

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