Class ModelAndView
java.lang.Object
org.springframework.web.servlet.ModelAndView
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:
- 
Constructor SummaryConstructorsConstructorDescriptionDefault constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.ModelAndView(@Nullable String viewName, @Nullable Map<String, ?> model, @Nullable HttpStatusCode status) Create a new ModelAndView given a view name, model, and HTTP status.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, @Nullable Map<String, ?> model) Create a new ModelAndView given a view name and a model.ModelAndView(String viewName, HttpStatusCode 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, @Nullable Map<String, ?> model) Create a new ModelAndView given a View object and a model.
- 
Method SummaryModifier and TypeMethodDescriptionaddAllObjects(@Nullable Map<String, ?> modelMap) Add all attributes contained in the provided Map to the model.Add an attribute to the model using parameter name generation.Add an attribute to the model.voidclear()Clear the state of this ModelAndView object.getModel()Return the model map.Return the model map.Return the underlyingModelMapinstance (nevernull).Return the configured HTTP status for the response, if any.getView()Return the View object, ornullif we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.Return the view name to be resolved by the DispatcherServlet via a ViewResolver, ornullif we are using a View object.booleanhasView()Indicate whether thisModelAndViewhas a view, either as a view name or as a directViewinstance.booleanisEmpty()Return whether this ModelAndView object is empty, i.e.booleanReturn whether we use a view reference, i.e.voidsetStatus(@Nullable HttpStatusCode status) Set the HTTP status to use for the response.voidSet a View object for this ModelAndView.voidsetViewName(@Nullable String viewName) Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver.toString()Return diagnostic information about this model and view.booleanReturn whether this ModelAndView object is empty as a result of a call toclear()i.e.
- 
Constructor Details- 
ModelAndViewpublic ModelAndView()Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.- See Also:
 
- 
ModelAndViewConvenient 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:
 
- 
ModelAndViewConvenient constructor when there is no model data to expose. Can also be used in conjunction withaddObject.- Parameters:
- view- the View object to render
- See Also:
 
- 
ModelAndViewCreate 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 ViewResolver
- model- a Map of model names (Strings) to model objects (Objects). Model entries may not be- null, but the model Map may be- nullif there is no model data.
 
- 
ModelAndViewCreate 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- the View object to render
- model- a Map of model names (Strings) to model objects (Objects). Model entries may not be- null, but the model Map may be- nullif there is no model data.
 
- 
ModelAndViewCreate 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 ViewResolver
- status- an HTTP status code to use for the response (to be set just prior to View rendering)
- Since:
- 4.3.8
 
- 
ModelAndViewpublic ModelAndView(@Nullable String viewName, @Nullable Map<String, ?> model, @Nullable HttpStatusCode 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 ViewResolver
- model- a Map of model names (Strings) to model objects (Objects). Model entries may not be- null, but the model Map may be- nullif 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
 
- 
ModelAndViewConvenient constructor to take a single model object.- Parameters:
- viewName- name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
- modelName- name of the single entry in the model
- modelObject- the single model object
 
- 
ModelAndView
 
- 
- 
Method Details- 
setViewName
- 
getViewName
- 
setView
- 
getView
- 
hasViewpublic boolean hasView()Indicate whether thisModelAndViewhas a view, either as a view name or as a directViewinstance.
- 
isReferencepublic boolean isReference()Return whether we use a view reference, i.e.trueif the view has been specified via a name to be resolved by the DispatcherServlet via a ViewResolver.
- 
getModelInternal
- 
getModelMapReturn the underlyingModelMapinstance (nevernull).
- 
getModel
- 
setStatusSet the HTTP status to use for the response.The response status is set just prior to View rendering. - Since:
- 4.3
 
- 
getStatusReturn the configured HTTP status for the response, if any.- Since:
- 4.3
 
- 
addObjectAdd an attribute to the model.- Parameters:
- attributeName- name of the object to add to the model (never- null)
- attributeValue- object to add to the model (can be- null)
- See Also:
 
- 
addObjectAdd an attribute to the model using parameter name generation.- Parameters:
- attributeValue- the object to add to the model (never- null)
- See Also:
 
- 
addAllObjectsAdd all attributes contained in the provided Map to the model.- Parameters:
- modelMap- a Map of attributeName → attributeValue pairs
- See Also:
 
- 
clearpublic 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 postHandlemethod of a HandlerInterceptor.- See Also:
 
- 
isEmptypublic 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
- 
toString
 
-