public class ModelAndView extends 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(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
,
Controller.handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
Constructor and Description |
---|
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(String viewName,
HttpStatus status)
Create a new ModelAndView given a view name and HTTP status.
|
ModelAndView(String viewName,
Map<String,?> model)
Create a new ModelAndView given a view name and a model.
|
ModelAndView(String viewName,
Map<String,?> model,
HttpStatus status)
Create a new ModelAndView given a view name, model, and HTTP status.
|
ModelAndView(String viewName,
String modelName,
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,
Map<String,?> model)
Create a new ModelAndView given a View object and a model.
|
ModelAndView(View view,
String modelName,
Object modelObject)
Convenient constructor to take a single model object.
|
Modifier and Type | Method and Description |
---|---|
ModelAndView |
addAllObjects(Map<String,?> modelMap)
Add all attributes contained in the provided Map to the model.
|
ModelAndView |
addObject(Object attributeValue)
Add an attribute to the model using parameter name generation.
|
ModelAndView |
addObject(String attributeName,
Object attributeValue)
Add an attribute to the model.
|
void |
clear()
Clear the state of this ModelAndView object.
|
Map<String,Object> |
getModel()
Return the model map.
|
protected Map<String,Object> |
getModelInternal()
Return the model map.
|
ModelMap |
getModelMap()
Return the underlying
ModelMap instance (never null ). |
HttpStatus |
getStatus()
Return the configured HTTP status for the response, if any.
|
View |
getView()
Return the View object, or
null if we are using a view name
to be resolved by the DispatcherServlet via a ViewResolver. |
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 |
setStatus(HttpStatus status)
Set the HTTP status to use for the response.
|
void |
setView(View view)
Set a View object for this ModelAndView.
|
void |
setViewName(String viewName)
Set a view name for this ModelAndView, to be resolved by the
DispatcherServlet 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. |
public ModelAndView()
setView(View)
,
setViewName(String)
public ModelAndView(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
- the View object to renderaddObject(java.lang.String, java.lang.Object)
public ModelAndView(String viewName, @Nullable Map<String,?> model)
viewName
- name of the View to render, to be resolved
by the DispatcherServlet's ViewResolvermodel
- 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.public ModelAndView(View view, @Nullable Map<String,?> model)
view
- the View object to rendermodel
- 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.public ModelAndView(String viewName, HttpStatus status)
viewName
- name of the View to render, to be resolved
by the DispatcherServlet's ViewResolverstatus
- an HTTP status code to use for the response
(to be set just prior to View rendering)public ModelAndView(@Nullable String viewName, @Nullable Map<String,?> model, @Nullable HttpStatus status)
viewName
- name of the View to render, to be resolved
by the DispatcherServlet's ViewResolvermodel
- 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
- an HTTP status code to use for the response
(to be set just prior to View rendering)public ModelAndView(String viewName, String modelName, 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 void setViewName(@Nullable String viewName)
@Nullable public String getViewName()
null
if we are using a View object.public void setView(@Nullable View view)
@Nullable 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.@Nullable protected Map<String,Object> getModelInternal()
null
.
Called by DispatcherServlet for evaluation of the model.public ModelMap getModelMap()
ModelMap
instance (never null
).public Map<String,Object> getModel()
null
.
To be called by application code for modifying the model.public void setStatus(@Nullable HttpStatus status)
The response status is set just prior to View rendering.
@Nullable public HttpStatus getStatus()
public ModelAndView addObject(String attributeName, @Nullable Object attributeValue)
attributeName
- name of the object to add to the model (never null
)attributeValue
- object to add to the model (can be null
)ModelMap.addAttribute(String, Object)
,
getModelMap()
public ModelAndView addObject(Object attributeValue)
attributeValue
- the object to add to the model (never null
)ModelMap.addAttribute(Object)
,
getModelMap()
public ModelAndView addAllObjects(@Nullable Map<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()