Package org.springframework.web.servlet
Class ModelAndView
- java.lang.Object
- org.springframework.web.servlet.ModelAndView
public class ModelAndView extends Object
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.
- Author:
- Rod Johnson, Juergen Hoeller, Rob Harrop, Rossen Stoyanchev
- See Also:
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 Summary
Constructors Constructor 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, String modelName, Object modelObject)
Convenient constructor to take a single model object.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, HttpStatus status)
Create a new ModelAndView given a view name and HTTP status.ModelAndView(View view)
Convenient constructor when there is no model data to expose.ModelAndView(View view, String modelName, Object modelObject)
Convenient constructor to take a single model object.ModelAndView(View view, Map<String,?> model)
Create a new ModelAndView given a View object and a model.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 underlyingModelMap
instance (nevernull
).HttpStatus
getStatus()
Return the configured HTTP status for the response, if any.View
getView()
Return the View object, ornull
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, ornull
if we are using a View object.boolean
hasView()
Indicate whether or not thisModelAndView
has a view, either as a view name or as a directView
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 toclear()
i.e.
Constructor Detail
ModelAndView
public ModelAndView()
Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.- See Also:
setView(View)
,setViewName(String)
ModelAndView
public ModelAndView(String viewName)
Convenient constructor when there is no model data to expose. Can also be used in conjunction withaddObject
.- Parameters:
viewName
- name of the View to render, to be resolved by the DispatcherServlet's ViewResolver- See Also:
addObject(java.lang.String, java.lang.Object)
ModelAndView
public ModelAndView(View view)
Convenient constructor when there is no model data to expose. Can also be used in conjunction withaddObject
.- Parameters:
view
- View object to render- See Also:
addObject(java.lang.String, java.lang.Object)
ModelAndView
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 DispatcherServlet's ViewResolvermodel
- Map of model names (Strings) to model objects (Objects). Model entries may not benull
, but the model Map may benull
if there is no model data.
ModelAndView
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 object to rendermodel
- Map of model names (Strings) to model objects (Objects). Model entries may not benull
, but the model Map may benull
if there is no model data.
ModelAndView
public ModelAndView(String viewName, HttpStatus status)
Create a new ModelAndView given a view name and HTTP status.- Parameters:
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)- Since:
- 4.3.8
ModelAndView
public ModelAndView(String viewName, Map<String,?> model, HttpStatus status)
Create a new ModelAndView given a view name, model, and HTTP status.- Parameters:
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 benull
, but the model Map may benull
if there is no model data.status
- an HTTP status code to use for the response (to be set just prior to View rendering)- Since:
- 4.3
ModelAndView
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 DispatcherServlet's ViewResolvermodelName
- name of the single entry in the modelmodelObject
- the single model object
ModelAndView
public ModelAndView(View view, String modelName, Object modelObject)
Convenient constructor to take a single model object.- Parameters:
view
- View object to rendermodelName
- name of the single entry in the modelmodelObject
- the single model object
Method Detail
setViewName
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.
getViewName
public String getViewName()
Return the view name to be resolved by the DispatcherServlet via a ViewResolver, ornull
if we are using a View object.
setView
public void setView(View view)
Set a View object for this ModelAndView. Will override any pre-existing view name or View.
getView
public View getView()
Return the View object, ornull
if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.
hasView
public boolean hasView()
Indicate whether or not thisModelAndView
has a view, either as a view name or as a directView
instance.
isReference
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.
getModelInternal
protected Map<String,Object> getModelInternal()
Return the model map. May returnnull
. Called by DispatcherServlet for evaluation of the model.
getModelMap
public ModelMap getModelMap()
Return the underlyingModelMap
instance (nevernull
).
getModel
public Map<String,Object> getModel()
Return the model map. Never returnsnull
. To be called by application code for modifying the model.
setStatus
public void setStatus(HttpStatus status)
Set the HTTP status to use for the response.The response status is set just prior to View rendering.
- Since:
- 4.3
getStatus
public HttpStatus getStatus()
Return the configured HTTP status for the response, if any.- Since:
- 4.3
addObject
public ModelAndView addObject(String attributeName, Object attributeValue)
Add an attribute to the model.- Parameters:
attributeName
- name of the object to add to the modelattributeValue
- object to add to the model (nevernull
)- See Also:
ModelMap.addAttribute(String, Object)
,getModelMap()
addObject
public ModelAndView addObject(Object attributeValue)
Add an attribute to the model using parameter name generation.- Parameters:
attributeValue
- the object to add to the model (nevernull
)- See Also:
ModelMap.addAttribute(Object)
,getModelMap()
addAllObjects
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- See Also:
ModelMap.addAllAttributes(Map)
,getModelMap()
clear
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.
isEmpty
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.
wasCleared
public boolean wasCleared()
Return whether this ModelAndView object is empty as a result of a call toclear()
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 toclear()
.- See Also:
clear()