Skip to content

Commit ea2943f

Browse files
committed
Polish ModelAndViewContainer and update tests
1 parent 670974d commit ea2943f

File tree

3 files changed

+156
-118
lines changed

3 files changed

+156
-118
lines changed

spring-web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,36 @@
4444
*/
4545
public 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+
62+
/**
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}.
73+
*/
74+
public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect) {
75+
this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect;
76+
}
6177

6278
/**
6379
* Set a view name to be resolved by the DispatcherServlet via a ViewResolver.
@@ -100,32 +116,10 @@ public boolean isViewReference() {
100116
}
101117

102118
/**
103-
* Signal a scenario where the request is handled directly.
104-
* <p>A {@link HandlerMethodReturnValueHandler} may use this flag to
105-
* indicate the response has been fully handled and view resolution
106-
* is not required (e.g. {@code @ResponseBody}).
107-
* <p>A {@link HandlerMethodArgumentResolver} may also use this flag
108-
* to indicate the presence of an argument (e.g.
109-
* {@code ServletResponse} or {@code OutputStream}) that may lead to
110-
* a complete response depending on the method return value.
111-
* <p>The default value is {@code true}.
112-
*/
113-
public void setRequestHandled(boolean requestHandled) {
114-
this.requestHandled = requestHandled;
115-
}
116-
117-
/**
118-
* Whether the request is handled directly.
119-
*/
120-
public boolean isRequestHandled() {
121-
return this.requestHandled;
122-
}
123-
124-
/**
125-
* Return the model to use: the "default" or the "redirect" model.
126-
* <p>The default model is used if {@code "redirectModelScenario=false"} or
127-
* if the redirect model is {@code null} (i.e. it wasn't declared as a
128-
* 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}.
129123
*/
130124
public ModelMap getModel() {
131125
if (useDefaultModel()) {
@@ -154,25 +148,13 @@ public void setRedirectModel(ModelMap redirectModel) {
154148
}
155149

156150
/**
157-
* Signal the conditions are in place for using a redirect model.
158-
* Typically that means the controller has returned a redirect instruction.
151+
* Whether the controller has returned a redirect instruction, e.g. a
152+
* "redirect:" prefixed view name, a RedirectView instance, etc.
159153
*/
160154
public void setRedirectModelScenario(boolean redirectModelScenario) {
161155
this.redirectModelScenario = redirectModelScenario;
162156
}
163157

164-
/**
165-
* When set to {@code true} the default model is never used in a redirect
166-
* scenario. So if a redirect model is not available, an empty model is
167-
* used instead.
168-
* <p>When set to {@code false} the default model can be used in a redirect
169-
* scenario if a redirect model is not available.
170-
* <p>The default setting is {@code false}.
171-
*/
172-
public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect) {
173-
this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect;
174-
}
175-
176158
/**
177159
* Return the {@link SessionStatus} instance to use that can be used to
178160
* signal that session processing is complete.
@@ -181,6 +163,24 @@ public SessionStatus getSessionStatus() {
181163
return this.sessionStatus;
182164
}
183165

166+
/**
167+
* Whether the request has been handled fully within the handler, e.g.
168+
* {@code @ResponseBody} method, and therefore view resolution is not
169+
* necessary. This flag can also be set when controller methods declare an
170+
* argument of type {@code ServletResponse} or {@code OutputStream}).
171+
* <p>The default value is {@code false}.
172+
*/
173+
public void setRequestHandled(boolean requestHandled) {
174+
this.requestHandled = requestHandled;
175+
}
176+
177+
/**
178+
* Whether the request has been handled fully within the handler.
179+
*/
180+
public boolean isRequestHandled() {
181+
return this.requestHandled;
182+
}
183+
184184
/**
185185
* Add the supplied attribute to the underlying model.
186186
* A shortcut for {@code getModel().addAttribute(String, Object)}.

0 commit comments

Comments
 (0)