1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
44
44
*/
45
45
public class ModelAndViewContainer {
46
46
47
- private Object view ;
47
+ private boolean ignoreDefaultModelOnRedirect = false ;
48
48
49
- private boolean requestHandled = false ;
49
+ private Object view ;
50
50
51
51
private final ModelMap defaultModel = new BindingAwareModelMap ();
52
52
53
53
private ModelMap redirectModel ;
54
54
55
55
private boolean redirectModelScenario = false ;
56
56
57
- private boolean ignoreDefaultModelOnRedirect = false ;
58
-
59
57
private final SessionStatus sessionStatus = new SimpleSessionStatus ();
60
58
59
+ private boolean requestHandled = false ;
60
+
61
+
61
62
/**
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}.
63
73
*/
64
- public ModelAndViewContainer () {
74
+ public void setIgnoreDefaultModelOnRedirect (boolean ignoreDefaultModelOnRedirect ) {
75
+ this .ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect ;
65
76
}
66
77
67
78
/**
@@ -105,47 +116,28 @@ public boolean isViewReference() {
105
116
}
106
117
107
118
/**
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}.
134
123
*/
135
124
public ModelMap getModel () {
136
125
if (useDefaultModel ()) {
137
126
return this .defaultModel ;
138
127
}
139
128
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 ;
141
133
}
142
134
}
143
135
144
136
/**
145
137
* Whether to use the default model or the redirect model.
146
138
*/
147
139
private boolean useDefaultModel () {
148
- return !this .redirectModelScenario || (( this .redirectModel == null ) && !this .ignoreDefaultModelOnRedirect );
140
+ return ( !this .redirectModelScenario || (this .redirectModel == null && !this .ignoreDefaultModelOnRedirect ) );
149
141
}
150
142
151
143
/**
@@ -159,31 +151,37 @@ public void setRedirectModel(ModelMap redirectModel) {
159
151
}
160
152
161
153
/**
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 .
164
156
*/
165
157
public void setRedirectModelScenario (boolean redirectModelScenario ) {
166
158
this .redirectModelScenario = redirectModelScenario ;
167
159
}
168
160
169
161
/**
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.
176
164
*/
177
- public void setIgnoreDefaultModelOnRedirect ( boolean ignoreDefaultModelOnRedirect ) {
178
- this .ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect ;
165
+ public SessionStatus getSessionStatus ( ) {
166
+ return this .sessionStatus ;
179
167
}
180
168
181
169
/**
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}.
184
175
*/
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 ;
187
185
}
188
186
189
187
/**
@@ -243,6 +241,7 @@ public boolean containsAttribute(String name) {
243
241
return getModel ().containsAttribute (name );
244
242
}
245
243
244
+
246
245
/**
247
246
* Return diagnostic information.
248
247
*/
0 commit comments