public class ModelAndView
extends java.lang.Object
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.
DispatcherServlet
,
ViewResolver
,
HandlerAdapter.handle(HttpServletRequest, HttpServletResponse, java.lang.Object)
,
Controller.handleRequest(HttpServletRequest, HttpServletResponse)
Modifier and Type | Field and Description |
---|---|
private boolean |
cleared
Indicates whether or not this instance has been cleared with a call to
clear() |
private ModelMap |
model
Model Map
|
private java.lang.Object |
view
View instance or view name String
|
Constructor and Description |
---|
ModelAndView()
Default constructor for bean-style usage: populating bean
properties instead of passing in constructor arguments.
|
ModelAndView(java.lang.String viewName)
Convenient constructor when there is no model data to expose.
|
ModelAndView(java.lang.String viewName,
java.util.Map<java.lang.String,?> model)
Creates new ModelAndView given a view name and a model.
|
ModelAndView(java.lang.String viewName,
java.lang.String modelName,
java.lang.Object modelObject)
Convenient constructor to take a single model object.
|
ModelAndView(View view)
Convenient constructor when there is no model data to expose.
|
ModelAndView(View view,
java.util.Map<java.lang.String,?> model)
Creates new ModelAndView given a View object and a model.
|
ModelAndView(View view,
java.lang.String modelName,
java.lang.Object modelObject)
Convenient constructor to take a single model object.
|
Modifier and Type | Method and Description |
---|---|
ModelAndView |
addAllObjects(java.util.Map<java.lang.String,?> modelMap)
Add all attributes contained in the provided Map to the model.
|
ModelAndView |
addObject(java.lang.Object attributeValue)
Add an attribute to the model using parameter name generation.
|
ModelAndView |
addObject(java.lang.String attributeName,
java.lang.Object attributeValue)
Add an attribute to the model.
|
void |
clear()
Clear the state of this ModelAndView object.
|
java.util.Map<java.lang.String,java.lang.Object> |
getModel()
Return the model map.
|
protected java.util.Map<java.lang.String,java.lang.Object> |
getModelInternal()
Return the model map.
|
ModelMap |
getModelMap()
Return the underlying
ModelMap instance (never null ). |
View |
getView()
Return the View object, or
null if we are using a view name
to be resolved by the DispatcherServlet via a ViewResolver. |
java.lang.String |
getViewName()
Return the view name to be resolved by the DispatcherServlet
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(View view)
Set a View object for this ModelAndView.
|
void |
setViewName(java.lang.String viewName)
Set a view name for this ModelAndView, to be resolved by the
DispatcherServlet via a ViewResolver.
|
java.lang.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. |
private java.lang.Object view
private ModelMap model
private boolean cleared
clear()
public ModelAndView()
setView(View)
,
setViewName(String)
public ModelAndView(java.lang.String viewName)
addObject
.viewName
- name of the View to render, to be resolved
by the DispatcherServlet's ViewResolveraddObject(java.lang.String, java.lang.Object)
public ModelAndView(View view)
addObject
.view
- View object to renderaddObject(java.lang.String, java.lang.Object)
public ModelAndView(java.lang.String viewName, java.util.Map<java.lang.String,?> model)
viewName
- name of the View to render, to be resolved
by the DispatcherServlet's ViewResolvermodel
- 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(View view, java.util.Map<java.lang.String,?> model)
view
- View object to rendermodel
- 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(java.lang.String viewName, java.lang.String modelName, java.lang.Object modelObject)
viewName
- name of the View to render, to be resolved
by the DispatcherServlet's ViewResolvermodelName
- name of the single entry in the modelmodelObject
- the single model objectpublic ModelAndView(View view, java.lang.String modelName, java.lang.Object modelObject)
view
- View object to rendermodelName
- name of the single entry in the modelmodelObject
- the single model objectpublic void setViewName(java.lang.String viewName)
public java.lang.String getViewName()
null
if we are using a View object.public void setView(View view)
public View getView()
null
if we are using a view name
to be resolved by the DispatcherServlet via a ViewResolver.public boolean hasView()
ModelAndView
has a view, either
as a view name or as a direct View
instance.public boolean isReference()
true
if the view has been specified via a name to be resolved by the
DispatcherServlet via a ViewResolver.protected java.util.Map<java.lang.String,java.lang.Object> getModelInternal()
null
.
Called by DispatcherServlet for evaluation of the model.public ModelMap getModelMap()
ModelMap
instance (never null
).public java.util.Map<java.lang.String,java.lang.Object> getModel()
null
.
To be called by application code for modifying the model.public ModelAndView addObject(java.lang.String attributeName, java.lang.Object attributeValue)
attributeName
- name of the object to add to the modelattributeValue
- object to add to the model (never null
)ModelMap.addAttribute(String, Object)
,
getModelMap()
public ModelAndView addObject(java.lang.Object attributeValue)
attributeValue
- the object to add to the model (never null
)ModelMap.addAttribute(Object)
,
getModelMap()
public ModelAndView addAllObjects(java.util.Map<java.lang.String,?> modelMap)
modelMap
- a Map of attributeName -> attributeValue pairsModelMap.addAllAttributes(Map)
,
getModelMap()
public void clear()
Can be used to suppress rendering of a given ModelAndView object
in the postHandle
method of a HandlerInterceptor.
public boolean isEmpty()
public boolean wasCleared()
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()
.
clear()
public java.lang.String toString()
toString
in class java.lang.Object