Top Description Fields Constructors Methods
org.springframework.web.method.support

public Class ModelAndViewContainer

extends Object
Class Inheritance
Imports
java.util.HashSet, .Map, .Set, org.springframework.http.HttpStatusCode, org.springframework.lang.Nullable, org.springframework.ui.Model, .ModelMap, org.springframework.validation.support.BindingAwareModelMap, org.springframework.web.bind.support.SessionStatus, .SimpleSessionStatus

Records model and view related decisions made by HandlerMethodArgumentResolvers and HandlerMethodReturnValueHandlers during the course of invocation of a controller method.

The setRequestHandled flag can be used to indicate the request has been handled directly and view resolution is not required.

A default Model is automatically created at instantiation. An alternate model instance may be provided via setRedirectModel for use in a redirect scenario. When setRedirectModelScenario is set to true signaling a redirect scenario, the getModel() returns the redirect model instead of the default model.

Authors
Rossen Stoyanchev, Juergen Hoeller
Since
3.1

Field Summary

Modifier and TypeField and Description
private final Set<String>
private final ModelMap
private boolean
private final Set<String>
private ModelMap
private boolean
private boolean
private final SessionStatus
private HttpStatusCode
private Object

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public ModelAndViewContainer
addAllAttributes(Map<String, ?> attributes)

Copy all attributes to the underlying model.

public ModelAndViewContainer
addAttribute(String name, Object value)

Add the supplied attribute to the underlying model.

public ModelAndViewContainer
addAttribute(Object value)

Add the supplied attribute to the underlying model.

public boolean
containsAttribute(String name)

Whether the underlying model contains the given attribute name.

public ModelMap

Returns:

the default model (never null)
getDefaultModel
()

Return the "default" model created at instantiation.

public ModelMap
getModel()

Return the model to use -- either the "default" or the "redirect" model.

public SessionStatus
getSessionStatus()

Return the SessionStatus instance to use that can be used to signal that session processing is complete.

public HttpStatusCode
getStatus()

Return the configured HTTP status, if any.

public Object
getView()

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

public String
getViewName()

Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if a View object is set.

public boolean
isBindingDisabled(String name)

Whether binding is disabled for the given model attribute.

public boolean
isRequestHandled()

Whether the request has been handled fully within the handler.

public boolean
isViewReference()

Whether the view is a view reference specified via a name to be resolved by the DispatcherServlet via a ViewResolver.

public ModelAndViewContainer
mergeAttributes(Map<String, ?> attributes)

Copy attributes in the supplied Map with existing objects of the same name taking precedence (i.e. not getting replaced).

public ModelAndViewContainer
removeAttributes(Map<String, ?> attributes)

Remove the given attributes from the model.

public void
setBinding(String
the name of the attribute
attributeName
,
boolean enabled)

Register whether data binding should occur for a corresponding model attribute, corresponding to an @ModelAttribute(binding=true/false) declaration.

public void
setBindingDisabled(String
the name of the attribute
attributeName
)

Programmatically register an attribute for which data binding should not occur, not even for a subsequent @ModelAttribute declaration.

public void
setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)

Deprecated since 6.0. as of 6.0 without a replacement; once removed, the default model will always be ignored on redirect
By default, the content of the "default" model is used both during rendering and redirect scenarios.
public void
setRedirectModel(ModelMap redirectModel)

Provide a separate model instance to use in a redirect scenario.

public void
setRedirectModelScenario(boolean redirectModelScenario)

Whether the controller has returned a redirect instruction, e.g. a "redirect:" prefixed view name, a RedirectView instance, etc.

public void
setRequestHandled(boolean requestHandled)

Whether the request has been handled fully within the handler, e.g. @ResponseBody method, and therefore view resolution is not necessary.

public void
setStatus(HttpStatusCode status)

Provide an HTTP status that will be passed on to with the ModelAndView used for view rendering purposes.

public void
setView(Object view)

Set a View object to be used by the DispatcherServlet.

public void
setViewName(String viewName)

Set a view name to be resolved by the DispatcherServlet via a ViewResolver.

public String
toString()

Overrides java.lang.Object.toString.

Return diagnostic information.
private boolean
useDefaultModel()

Whether to use the default model or the redirect model.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

bindingDisabledback to summary
private final Set<String> bindingDisabled
defaultModelback to summary
private final ModelMap defaultModel
ignoreDefaultModelOnRedirectback to summary
private boolean ignoreDefaultModelOnRedirect
noBindingback to summary
private final Set<String> noBinding
redirectModelback to summary
private ModelMap redirectModel
Annotations
@Nullable
redirectModelScenarioback to summary
private boolean redirectModelScenario
requestHandledback to summary
private boolean requestHandled
sessionStatusback to summary
private final SessionStatus sessionStatus
statusback to summary
private HttpStatusCode status
Annotations
@Nullable
viewback to summary
private Object view
Annotations
@Nullable

Constructor Detail

ModelAndViewContainerback to summary
public ModelAndViewContainer()

Method Detail

addAllAttributesback to summary
public ModelAndViewContainer addAllAttributes(Map<String, ?> attributes)

Copy all attributes to the underlying model. A shortcut for getModel().addAllAttributes(Map).

addAttributeback to summary
public ModelAndViewContainer addAttribute(String name, Object value)

Add the supplied attribute to the underlying model. A shortcut for getModel().addAttribute(String, Object).

addAttributeback to summary
public ModelAndViewContainer addAttribute(Object value)

Add the supplied attribute to the underlying model. A shortcut for getModel().addAttribute(Object).

containsAttributeback to summary
public boolean containsAttribute(String name)

Whether the underlying model contains the given attribute name. A shortcut for getModel().containsAttribute(String).

getDefaultModelback to summary
public ModelMap getDefaultModel()

Return the "default" model created at instantiation.

In general it is recommended to use getModel() instead which returns either the "default" model (template rendering) or the "redirect" model (redirect URL preparation). Use of this method may be needed for advanced cases when access to the "default" model is needed regardless, e.g. to save model attributes specified via @SessionAttributes.

Returns:ModelMap

the default model (never null)

Since
4.1.4
getModelback to summary
public ModelMap getModel()

Return the model to use -- either the "default" or the "redirect" model. The default model is used if redirectModelScenario=false or there is no redirect model (i.e. RedirectAttributes was not declared as a method argument) and ignoreDefaultModelOnRedirect=false.

getSessionStatusback to summary
public SessionStatus getSessionStatus()

Return the SessionStatus instance to use that can be used to signal that session processing is complete.

getStatusback to summary
public HttpStatusCode getStatus()

Return the configured HTTP status, if any.

Annotations
@Nullable
Since
4.3
getViewback to summary
public Object getView()

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

Annotations
@Nullable
getViewNameback to summary
public String getViewName()

Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if a View object is set.

Annotations
@Nullable
isBindingDisabledback to summary
public boolean isBindingDisabled(String name)

Whether binding is disabled for the given model attribute.

Since
4.3
isRequestHandledback to summary
public boolean isRequestHandled()

Whether the request has been handled fully within the handler.

isViewReferenceback to summary
public boolean isViewReference()

Whether the view is a view reference specified via a name to be resolved by the DispatcherServlet via a ViewResolver.

mergeAttributesback to summary
public ModelAndViewContainer mergeAttributes(Map<String, ?> attributes)

Copy attributes in the supplied Map with existing objects of the same name taking precedence (i.e. not getting replaced). A shortcut for getModel().mergeAttributes(Map<String, ?>).

removeAttributesback to summary
public ModelAndViewContainer removeAttributes(Map<String, ?> attributes)

Remove the given attributes from the model.

setBindingback to summary
public void setBinding(String attributeName, boolean enabled)

Register whether data binding should occur for a corresponding model attribute, corresponding to an @ModelAttribute(binding=true/false) declaration.

Note

While this flag will be taken into account by isBindingDisabled, a hard setBindingDisabled declaration will always override it.

Parameters
attributeName:String

the name of the attribute

Since
4.3.13
setBindingDisabledback to summary
public void setBindingDisabled(String attributeName)

Programmatically register an attribute for which data binding should not occur, not even for a subsequent @ModelAttribute declaration.

Parameters
attributeName:String

the name of the attribute

Since
4.3
setIgnoreDefaultModelOnRedirectback to summary
public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)

Deprecated

since 6.0.

as of 6.0 without a replacement; once removed, the default model will always be ignored on redirect

By default, the content of the "default" model is used both during rendering and redirect scenarios. Alternatively controller methods can declare an argument of type RedirectAttributes and use it to provide attributes to prepare the redirect URL.

Setting this flag to true guarantees the "default" model is never used in a redirect scenario even if a RedirectAttributes argument is not declared. Setting it to false means the "default" model may be used in a redirect if the controller method doesn't declare a RedirectAttributes argument.

As of 6.0, this property is set to true by default.

Annotations
@Deprecated
since:6.0
setRedirectModelback to summary
public void setRedirectModel(ModelMap redirectModel)

Provide a separate model instance to use in a redirect scenario.

The provided additional model however is not used unless setRedirectModelScenario gets set to true to signal an actual redirect scenario.

setRedirectModelScenarioback to summary
public void setRedirectModelScenario(boolean redirectModelScenario)

Whether the controller has returned a redirect instruction, e.g. a "redirect:" prefixed view name, a RedirectView instance, etc.

setRequestHandledback to summary
public void setRequestHandled(boolean requestHandled)

Whether the request has been handled fully within the handler, e.g. @ResponseBody method, and therefore view resolution is not necessary. This flag can also be set when controller methods declare an argument of type ServletResponse or OutputStream).

The default value is false.

setStatusback to summary
public void setStatus(HttpStatusCode status)

Provide an HTTP status that will be passed on to with the ModelAndView used for view rendering purposes.

Since
4.3
setViewback to summary
public void setView(Object view)

Set a View object to be used by the DispatcherServlet. Will override any pre-existing view name or View.

setViewNameback to summary
public void setViewName(String viewName)

Set a view name to be resolved by the DispatcherServlet via a ViewResolver. Will override any pre-existing view name or View.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Return diagnostic information.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
useDefaultModelback to summary
private boolean useDefaultModel()

Whether to use the default model or the redirect model.