11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
4444 */
4545public class ModelAndViewContainer {
4646
47- private Object view ;
47+ private boolean ignoreDefaultModelOnRedirect = false ;
4848
49- private boolean requestHandled = false ;
49+ private Object view ;
5050
5151 private final ModelMap defaultModel = new BindingAwareModelMap ();
5252
5353 private ModelMap redirectModel ;
5454
5555 private boolean redirectModelScenario = false ;
5656
57- private boolean ignoreDefaultModelOnRedirect = false ;
58-
5957 private final SessionStatus sessionStatus = new SimpleSessionStatus ();
6058
59+ private boolean requestHandled = false ;
60+
61+
6162 /**
62- * Create a new instance.
63+ * By default the content of the "default" model is used both during
64+ * rendering and redirect scenarios. Alternatively controller methods
65+ * can declare an argument of type {@code RedirectAttributes} and use
66+ * it to provide attributes to prepare the redirect URL.
67+ * <p>Setting this flag to {@code true} guarantees the "default" model is
68+ * never used in a redirect scenario even if a RedirectAttributes argument
69+ * is not declared. Setting it to {@code false} means the "default" model
70+ * may be used in a redirect if the controller method doesn't declare a
71+ * RedirectAttributes argument.
72+ * <p>The default setting is {@code false}.
6373 */
64- public ModelAndViewContainer () {
74+ public void setIgnoreDefaultModelOnRedirect (boolean ignoreDefaultModelOnRedirect ) {
75+ this .ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect ;
6576 }
6677
6778 /**
@@ -105,47 +116,28 @@ public boolean isViewReference() {
105116 }
106117
107118 /**
108- * Signal a scenario where the request is handled directly.
109- * <p>A {@link HandlerMethodReturnValueHandler} may use this flag to
110- * indicate the response has been fully handled and view resolution
111- * is not required (e.g. {@code @ResponseBody}).
112- * <p>A {@link HandlerMethodArgumentResolver} may also use this flag
113- * to indicate the presence of an argument (e.g.
114- * {@code ServletResponse} or {@code OutputStream}) that may lead to
115- * a complete response depending on the method return value.
116- * <p>The default value is {@code true}.
117- */
118- public void setRequestHandled (boolean requestHandled ) {
119- this .requestHandled = requestHandled ;
120- }
121-
122- /**
123- * Whether the request is handled directly.
124- */
125- public boolean isRequestHandled () {
126- return this .requestHandled ;
127- }
128-
129- /**
130- * Return the model to use: the "default" or the "redirect" model.
131- * <p>The default model is used if {@code "redirectModelScenario=false"} or
132- * if the redirect model is {@code null} (i.e. it wasn't declared as a
133- * method argument) and {@code ignoreDefaultModelOnRedirect=false}.
119+ * Return the model to use: either the "default" or the "redirect" model.
120+ * The default model is used if {@code redirectModelScenario=false} or
121+ * there is no redirect model (i.e. RedirectAttributes was not declared as
122+ * a method argument) and {@code ignoreDefaultModelOnRedirect=false}.
134123 */
135124 public ModelMap getModel () {
136125 if (useDefaultModel ()) {
137126 return this .defaultModel ;
138127 }
139128 else {
140- return (this .redirectModel != null ) ? this .redirectModel : new ModelMap ();
129+ if (this .redirectModel == null ) {
130+ this .redirectModel = new ModelMap ();
131+ }
132+ return this .redirectModel ;
141133 }
142134 }
143135
144136 /**
145137 * Whether to use the default model or the redirect model.
146138 */
147139 private boolean useDefaultModel () {
148- return !this .redirectModelScenario || (( this .redirectModel == null ) && !this .ignoreDefaultModelOnRedirect );
140+ return ( !this .redirectModelScenario || (this .redirectModel == null && !this .ignoreDefaultModelOnRedirect ) );
149141 }
150142
151143 /**
@@ -159,31 +151,37 @@ public void setRedirectModel(ModelMap redirectModel) {
159151 }
160152
161153 /**
162- * Signal the conditions are in place for using a redirect model.
163- * Typically that means the controller has returned a redirect instruction .
154+ * Whether the controller has returned a redirect instruction, e.g. a
155+ * "redirect:" prefixed view name, a RedirectView instance, etc .
164156 */
165157 public void setRedirectModelScenario (boolean redirectModelScenario ) {
166158 this .redirectModelScenario = redirectModelScenario ;
167159 }
168160
169161 /**
170- * When set to {@code true} the default model is never used in a redirect
171- * scenario. So if a redirect model is not available, an empty model is
172- * used instead.
173- * <p>When set to {@code false} the default model can be used in a redirect
174- * scenario if a redirect model is not available.
175- * <p>The default setting is {@code false}.
162+ * Return the {@link SessionStatus} instance to use that can be used to
163+ * signal that session processing is complete.
176164 */
177- public void setIgnoreDefaultModelOnRedirect ( boolean ignoreDefaultModelOnRedirect ) {
178- this .ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect ;
165+ public SessionStatus getSessionStatus ( ) {
166+ return this .sessionStatus ;
179167 }
180168
181169 /**
182- * Return the {@link SessionStatus} instance to use that can be used to
183- * signal that session processing is complete.
170+ * Whether the request has been handled fully within the handler, e.g.
171+ * {@code @ResponseBody} method, and therefore view resolution is not
172+ * necessary. This flag can also be set when controller methods declare an
173+ * argument of type {@code ServletResponse} or {@code OutputStream}).
174+ * <p>The default value is {@code false}.
184175 */
185- public SessionStatus getSessionStatus () {
186- return sessionStatus ;
176+ public void setRequestHandled (boolean requestHandled ) {
177+ this .requestHandled = requestHandled ;
178+ }
179+
180+ /**
181+ * Whether the request has been handled fully within the handler.
182+ */
183+ public boolean isRequestHandled () {
184+ return this .requestHandled ;
187185 }
188186
189187 /**
@@ -243,6 +241,7 @@ public boolean containsAttribute(String name) {
243241 return getModel ().containsAttribute (name );
244242 }
245243
244+
246245 /**
247246 * Return diagnostic information.
248247 */
0 commit comments