@@ -49,6 +49,8 @@ public abstract class AbstractView implements View, ApplicationContextAware {
49
49
50
50
private Charset defaultCharset = StandardCharsets .UTF_8 ;
51
51
52
+ private String requestContextAttribute ;
53
+
52
54
private ApplicationContext applicationContext ;
53
55
54
56
@@ -95,6 +97,21 @@ public Charset getDefaultCharset() {
95
97
return this .defaultCharset ;
96
98
}
97
99
100
+ /**
101
+ * Set the name of the RequestContext attribute for this view.
102
+ * Default is none.
103
+ */
104
+ public void setRequestContextAttribute (String requestContextAttribute ) {
105
+ this .requestContextAttribute = requestContextAttribute ;
106
+ }
107
+
108
+ /**
109
+ * Return the name of the RequestContext attribute, if any.
110
+ */
111
+ public String getRequestContextAttribute () {
112
+ return this .requestContextAttribute ;
113
+ }
114
+
98
115
@ Override
99
116
public void setApplicationContext (ApplicationContext applicationContext ) {
100
117
this .applicationContext = applicationContext ;
@@ -126,6 +143,12 @@ public Mono<Void> render(Map<String, ?> model, MediaType contentType,
126
143
}
127
144
128
145
Map <String , Object > mergedModel = getModelAttributes (model , exchange );
146
+
147
+ // Expose RequestContext?
148
+ if (this .requestContextAttribute != null ) {
149
+ mergedModel .put (this .requestContextAttribute , createRequestContext (exchange , mergedModel ));
150
+ }
151
+
129
152
return renderInternal (mergedModel , contentType , exchange );
130
153
}
131
154
@@ -145,6 +168,20 @@ protected Map<String, Object> getModelAttributes(Map<String, ?> model, ServerWeb
145
168
return attributes ;
146
169
}
147
170
171
+ /**
172
+ * Create a RequestContext to expose under the specified attribute name.
173
+ * <p>The default implementation creates a standard RequestContext instance for the
174
+ * given request and model. Can be overridden in subclasses for custom instances.
175
+ * @param exchange current exchange
176
+ * @param model combined output Map (never {@code null}),
177
+ * with dynamic values taking precedence over static attributes
178
+ * @return the RequestContext instance
179
+ * @see #setRequestContextAttribute
180
+ */
181
+ protected RequestContext createRequestContext (ServerWebExchange exchange , Map <String , Object > model ) {
182
+ return new RequestContext (exchange , model , this .applicationContext );
183
+ }
184
+
148
185
/**
149
186
* Subclasses must implement this method to actually render the view.
150
187
* @param renderAttributes combined output Map (never {@code null}),
0 commit comments