Skip to content

Commit 34eccbe

Browse files
committed
Remove final declaration from getMessageSource() and getLocale() methods
Includes checking the Servlet 3.0+ ServletRequest.getServletContext() method in the request-only constructors, being able to fall back to the root WebApplicationContext. See gh-32926
1 parent a582e48 commit 34eccbe

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,29 @@ public class RequestContext {
132132
* Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
133133
* <p>This only works with InternalResourceViews, as Errors instances are part of the model and not
134134
* normally exposed as request attributes. It will typically be used within JSPs or custom tags.
135-
* <p><b>Will only work within a DispatcherServlet request.</b>
136-
* Pass in a ServletContext to be able to fall back to the root WebApplicationContext.
135+
* <p>As of 6.2, this will work within a DispatcherServlet request as well as with the root
136+
* WebApplicationContext (outside a DispatcherServlet).
137137
* @param request current HTTP request
138138
* @see org.springframework.web.servlet.DispatcherServlet
139139
* @see #RequestContext(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletContext)
140140
*/
141141
public RequestContext(HttpServletRequest request) {
142-
this(request, null, null, null);
142+
this(request, null, request.getServletContext(), null);
143143
}
144144

145145
/**
146146
* Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
147147
* <p>This only works with InternalResourceViews, as Errors instances are part of the model and not
148148
* normally exposed as request attributes. It will typically be used within JSPs or custom tags.
149-
* <p><b>Will only work within a DispatcherServlet request.</b>
150-
* Pass in a ServletContext to be able to fall back to the root WebApplicationContext.
149+
* <p>As of 6.2, this will work within a DispatcherServlet request as well as with the root
150+
* WebApplicationContext (outside a DispatcherServlet).
151151
* @param request current HTTP request
152152
* @param response current HTTP response
153153
* @see org.springframework.web.servlet.DispatcherServlet
154154
* @see #RequestContext(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.ServletContext, Map)
155155
*/
156156
public RequestContext(HttpServletRequest request, HttpServletResponse response) {
157-
this(request, response, null, null);
157+
this(request, response, request.getServletContext(), null);
158158
}
159159

160160
/**
@@ -176,16 +176,16 @@ public RequestContext(HttpServletRequest request, @Nullable ServletContext servl
176176
/**
177177
* Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
178178
* <p>This works with all View implementations. It will typically be used by View implementations.
179-
* <p><b>Will only work within a DispatcherServlet request.</b>
180-
* Pass in a ServletContext to be able to fall back to the root WebApplicationContext.
179+
* <p>As of 6.2, this will work within a DispatcherServlet request as well as with the root
180+
* WebApplicationContext (outside a DispatcherServlet).
181181
* @param request current HTTP request
182182
* @param model the model attributes for the current view (can be {@code null},
183183
* using the request attributes for Errors retrieval)
184184
* @see org.springframework.web.servlet.DispatcherServlet
185185
* @see #RequestContext(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.ServletContext, Map)
186186
*/
187187
public RequestContext(HttpServletRequest request, @Nullable Map<String, Object> model) {
188-
this(request, null, null, model);
188+
this(request, null, request.getServletContext(), model);
189189
}
190190

191191
/**
@@ -281,13 +281,6 @@ public final WebApplicationContext getWebApplicationContext() {
281281
return this.webApplicationContext;
282282
}
283283

284-
/**
285-
* Return the current WebApplicationContext as MessageSource.
286-
*/
287-
public final MessageSource getMessageSource() {
288-
return this.webApplicationContext;
289-
}
290-
291284
/**
292285
* Return the model Map that this RequestContext encapsulates, if any.
293286
* @return the populated model Map, or {@code null} if none available
@@ -297,13 +290,22 @@ public final Map<String, Object> getModel() {
297290
return this.model;
298291
}
299292

293+
/**
294+
* Return the MessageSource to use (typically the current WebApplicationContext).
295+
* <p>Note: As of 6.2, this method is non-final and therefore overridable.
296+
*/
297+
public MessageSource getMessageSource() {
298+
return this.webApplicationContext;
299+
}
300+
300301
/**
301302
* Return the current Locale (falling back to the request locale; never {@code null}).
302303
* <p>Typically coming from a DispatcherServlet's {@link LocaleResolver}.
303304
* Also includes a fallback check for JSTL's Locale attribute.
305+
* <p>Note: As of 6.2, this method is non-final and therefore overridable.
304306
* @see RequestContextUtils#getLocale
305307
*/
306-
public final Locale getLocale() {
308+
public Locale getLocale() {
307309
return (this.locale != null ? this.locale : getFallbackLocale());
308310
}
309311

0 commit comments

Comments
 (0)