Spring Framework

org.springframework.web.method.support
Class ModelAndViewContainer

java.lang.Object
  extended by org.springframework.web.method.support.ModelAndViewContainer

public class ModelAndViewContainer
extends java.lang.Object

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

The setRequestHandled(boolean) 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(org.springframework.ui.ModelMap) for use in a redirect scenario. When setRedirectModelScenario(boolean) is set to true signalling a redirect scenario, the getModel() returns the redirect model instead of the default model.

Since:
3.1
Author:
Rossen Stoyanchev

Constructor Summary
ModelAndViewContainer()
          Create a new instance.
 
Method Summary
 ModelAndViewContainer addAllAttributes(java.util.Map<java.lang.String,?> attributes)
          Copy all attributes to the underlying model.
 ModelAndViewContainer addAttribute(java.lang.Object value)
          Add the supplied attribute to the underlying model.
 ModelAndViewContainer addAttribute(java.lang.String name, java.lang.Object value)
          Add the supplied attribute to the underlying model.
 boolean containsAttribute(java.lang.String name)
          Whether the underlying model contains the given attribute name.
 ModelMap getModel()
          Return the model to use: the "default" or the "redirect" model.
 SessionStatus getSessionStatus()
          Return the SessionStatus instance to use that can be used to signal that session processing is complete.
 java.lang.Object getView()
          Return the View object, or null if we 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 a View object is set.
 boolean isRequestHandled()
          Whether the request is handled directly.
 boolean isViewReference()
          Whether the view is a view reference specified via a name to be resolved by the DispatcherServlet via a ViewResolver.
 ModelAndViewContainer mergeAttributes(java.util.Map<java.lang.String,?> attributes)
          Copy attributes in the supplied Map with existing objects of the same name taking precedence (i.e.
 ModelAndViewContainer removeAttributes(java.util.Map<java.lang.String,?> attributes)
          Remove the given attributes from the model.
 void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)
          When set to true the default model is never used in a redirect scenario.
 void setRedirectModel(ModelMap redirectModel)
          Provide a separate model instance to use in a redirect scenario.
 void setRedirectModelScenario(boolean redirectModelScenario)
          Signal the conditions are in place for using a redirect model.
 void setRequestHandled(boolean requestHandled)
          Signal a scenario where the request is handled directly.
 void setView(java.lang.Object view)
          Set a View object to be used by the DispatcherServlet.
 void setViewName(java.lang.String viewName)
          Set a view name to be resolved by the DispatcherServlet via a ViewResolver.
 java.lang.String toString()
          Return diagnostic information.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ModelAndViewContainer

public ModelAndViewContainer()
Create a new instance.

Method Detail

setViewName

public void setViewName(java.lang.String viewName)
Set a view name to be resolved by the DispatcherServlet via a ViewResolver. Will override any pre-existing view name or View.


getViewName

public java.lang.String getViewName()
Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if a View object is set.


setView

public void setView(java.lang.Object view)
Set a View object to be used by the DispatcherServlet. Will override any pre-existing view name or View.


getView

public java.lang.Object getView()
Return the View object, or null if we using a view name to be resolved by the DispatcherServlet via a ViewResolver.


isViewReference

public boolean isViewReference()
Whether the view is a view reference specified via a name to be resolved by the DispatcherServlet via a ViewResolver.


setRequestHandled

public void setRequestHandled(boolean requestHandled)
Signal a scenario where the request is handled directly.

A HandlerMethodReturnValueHandler may use this flag to indicate the response has been fully handled and view resolution is not required (e.g. @ResponseBody).

A HandlerMethodArgumentResolver may also use this flag to indicate the presence of an argument (e.g. ServletResponse or OutputStream) that may lead to a complete response depending on the method return value.

The default value is true.


isRequestHandled

public boolean isRequestHandled()
Whether the request is handled directly.


getModel

public ModelMap getModel()
Return the model to use: the "default" or the "redirect" model.

The default model is used if "redirectModelScenario=false" or if the redirect model is null (i.e. it wasn't declared as a method argument) and ignoreDefaultModelOnRedirect=false.


setRedirectModel

public void setRedirectModel(ModelMap redirectModel)
Provide a separate model instance to use in a redirect scenario. The provided additional model however is not used used unless setRedirectModelScenario(boolean) gets set to true to signal a redirect scenario.


setRedirectModelScenario

public void setRedirectModelScenario(boolean redirectModelScenario)
Signal the conditions are in place for using a redirect model. Typically that means the controller has returned a redirect instruction.


setIgnoreDefaultModelOnRedirect

public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)
When set to true the default model is never used in a redirect scenario. So if a redirect model is not available, an empty model is used instead.

When set to false the default model can be used in a redirect scenario if a redirect model is not available.

The default setting is false.


getSessionStatus

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


addAttribute

public ModelAndViewContainer addAttribute(java.lang.String name,
                                          java.lang.Object value)
Add the supplied attribute to the underlying model. A shortcut for getModel().addAttribute(String, Object).


addAttribute

public ModelAndViewContainer addAttribute(java.lang.Object value)
Add the supplied attribute to the underlying model. A shortcut for getModel().addAttribute(Object).


addAllAttributes

public ModelAndViewContainer addAllAttributes(java.util.Map<java.lang.String,?> attributes)
Copy all attributes to the underlying model. A shortcut for getModel().addAllAttributes(Map).


mergeAttributes

public ModelAndViewContainer mergeAttributes(java.util.Map<java.lang.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, ?>).


removeAttributes

public ModelAndViewContainer removeAttributes(java.util.Map<java.lang.String,?> attributes)
Remove the given attributes from the model.


containsAttribute

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


toString

public java.lang.String toString()
Return diagnostic information.

Overrides:
toString in class java.lang.Object

Spring Framework