类 ModelAndViewContainer
- java.lang.Object
- org.springframework.web.method.support.ModelAndViewContainer
public class ModelAndViewContainer extends Object
Records model and view related decisions made byHandlerMethodArgumentResolversandHandlerMethodReturnValueHandlersduring 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
Modelis automatically created at instantiation. An alternate model instance may be provided viasetRedirectModel(org.springframework.ui.ModelMap)for use in a redirect scenario. WhensetRedirectModelScenario(boolean)is set totruesignalling a redirect scenario, thegetModel()returns the redirect model instead of the default model.- 从以下版本开始:
- 3.1
- 作者:
- Rossen Stoyanchev, Juergen Hoeller
构造器概要
构造器 构造器 说明 ModelAndViewContainer()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 ModelAndViewContaineraddAllAttributes(Map<String,?> attributes)Copy all attributes to the underlying model.ModelAndViewContaineraddAttribute(Object value)Add the supplied attribute to the underlying model.ModelAndViewContaineraddAttribute(String name, Object value)Add the supplied attribute to the underlying model.booleancontainsAttribute(String name)Whether the underlying model contains the given attribute name.ModelMapgetDefaultModel()Return the "default" model created at instantiation.ModelMapgetModel()Return the model to use -- either the "default" or the "redirect" model.SessionStatusgetSessionStatus()Return theSessionStatusinstance to use that can be used to signal that session processing is complete.HttpStatusgetStatus()Return the configured HTTP status, if any.ObjectgetView()Return the View object, ornullif we using a view name to be resolved by the DispatcherServlet via a ViewResolver.StringgetViewName()Return the view name to be resolved by the DispatcherServlet via a ViewResolver, ornullif a View object is set.booleanisBindingDisabled(String name)Whether binding is disabled for the given model attribute.booleanisRequestHandled()Whether the request has been handled fully within the handler.booleanisViewReference()Whether the view is a view reference specified via a name to be resolved by the DispatcherServlet via a ViewResolver.ModelAndViewContainermergeAttributes(Map<String,?> attributes)Copy attributes in the suppliedMapwith existing objects of the same name taking precedence (i.e. not getting replaced).ModelAndViewContainerremoveAttributes(Map<String,?> attributes)Remove the given attributes from the model.voidsetBinding(String attributeName, boolean enabled)Register whether data binding should occur for a corresponding model attribute, corresponding to an@ModelAttribute(binding=true/false)declaration.voidsetBindingDisabled(String attributeName)Programmatically register an attribute for which data binding should not occur, not even for a subsequent@ModelAttributedeclaration.voidsetIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)By default the content of the "default" model is used both during rendering and redirect scenarios.voidsetRedirectModel(ModelMap redirectModel)Provide a separate model instance to use in a redirect scenario.voidsetRedirectModelScenario(boolean redirectModelScenario)Whether the controller has returned a redirect instruction, e.g. a "redirect:" prefixed view name, a RedirectView instance, etc.voidsetRequestHandled(boolean requestHandled)Whether the request has been handled fully within the handler, e.g.voidsetStatus(HttpStatus status)Provide an HTTP status that will be passed on to with theModelAndViewused for view rendering purposes.voidsetView(Object view)Set a View object to be used by the DispatcherServlet.voidsetViewName(String viewName)Set a view name to be resolved by the DispatcherServlet via a ViewResolver.StringtoString()Return diagnostic information.
构造器详细资料
ModelAndViewContainer
public ModelAndViewContainer()
方法详细资料
setIgnoreDefaultModelOnRedirect
public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)
By default the content of the "default" model is used both during rendering and redirect scenarios. Alternatively controller methods can declare an argument of typeRedirectAttributesand use it to provide attributes to prepare the redirect URL.Setting this flag to
trueguarantees the "default" model is never used in a redirect scenario even if a RedirectAttributes argument is not declared. Setting it tofalsemeans the "default" model may be used in a redirect if the controller method doesn't declare a RedirectAttributes argument.The default setting is
false.
setViewName
public void setViewName(@Nullable 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
@Nullable public String getViewName()
Return the view name to be resolved by the DispatcherServlet via a ViewResolver, ornullif a View object is set.
setView
public void setView(@Nullable Object view)
Set a View object to be used by the DispatcherServlet. Will override any pre-existing view name or View.
getView
@Nullable public Object getView()
Return the View object, ornullif 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.
getModel
public ModelMap getModel()
Return the model to use -- either the "default" or the "redirect" model. The default model is used ifredirectModelScenario=falseor there is no redirect model (i.e. RedirectAttributes was not declared as a method argument) andignoreDefaultModelOnRedirect=false.
getDefaultModel
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.- 返回:
- the default model (never
null) - 从以下版本开始:
- 4.1.4
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 unless
setRedirectModelScenario(boolean)gets set totrueto signal an actual redirect scenario.
setRedirectModelScenario
public void setRedirectModelScenario(boolean redirectModelScenario)
Whether the controller has returned a redirect instruction, e.g. a "redirect:" prefixed view name, a RedirectView instance, etc.
setStatus
public void setStatus(@Nullable HttpStatus status)
Provide an HTTP status that will be passed on to with theModelAndViewused for view rendering purposes.- 从以下版本开始:
- 4.3
getStatus
@Nullable public HttpStatus getStatus()
Return the configured HTTP status, if any.- 从以下版本开始:
- 4.3
setBindingDisabled
public void setBindingDisabled(String attributeName)
Programmatically register an attribute for which data binding should not occur, not even for a subsequent@ModelAttributedeclaration.- 参数:
attributeName- the name of the attribute- 从以下版本开始:
- 4.3
isBindingDisabled
public boolean isBindingDisabled(String name)
Whether binding is disabled for the given model attribute.- 从以下版本开始:
- 4.3
setBinding
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(java.lang.String), a hardsetBindingDisabled(java.lang.String)declaration will always override it.- 参数:
attributeName- the name of the attribute- 从以下版本开始:
- 4.3.13
getSessionStatus
public SessionStatus getSessionStatus()
Return theSessionStatusinstance to use that can be used to signal that session processing is complete.
setRequestHandled
public void setRequestHandled(boolean requestHandled)
Whether the request has been handled fully within the handler, e.g.@ResponseBodymethod, and therefore view resolution is not necessary. This flag can also be set when controller methods declare an argument of typeServletResponseorOutputStream).The default value is
false.
isRequestHandled
public boolean isRequestHandled()
Whether the request has been handled fully within the handler.
addAttribute
public ModelAndViewContainer addAttribute(String name, @Nullable Object value)
Add the supplied attribute to the underlying model. A shortcut forgetModel().addAttribute(String, Object).
addAttribute
public ModelAndViewContainer addAttribute(Object value)
Add the supplied attribute to the underlying model. A shortcut forgetModel().addAttribute(Object).
addAllAttributes
public ModelAndViewContainer addAllAttributes(@Nullable Map<String,?> attributes)
Copy all attributes to the underlying model. A shortcut forgetModel().addAllAttributes(Map).
mergeAttributes
public ModelAndViewContainer mergeAttributes(@Nullable Map<String,?> attributes)
Copy attributes in the suppliedMapwith existing objects of the same name taking precedence (i.e. not getting replaced). A shortcut forgetModel().mergeAttributes(Map<String, ?>).
removeAttributes
public ModelAndViewContainer removeAttributes(@Nullable Map<String,?> attributes)
Remove the given attributes from the model.
containsAttribute
public boolean containsAttribute(String name)
Whether the underlying model contains the given attribute name. A shortcut forgetModel().containsAttribute(String).