From 34eccbe8e6d1ceb818a63ce41f3a9a2df9ae2b2a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 3 Jun 2024 15:27:33 +0200 Subject: [PATCH] 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 --- .../web/servlet/support/RequestContext.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java index d9d441e6c3..642f6e6a37 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java @@ -132,29 +132,29 @@ public class RequestContext { * Create a new RequestContext for the given request, using the request attributes for Errors retrieval. *

This only works with InternalResourceViews, as Errors instances are part of the model and not * normally exposed as request attributes. It will typically be used within JSPs or custom tags. - *

Will only work within a DispatcherServlet request. - * Pass in a ServletContext to be able to fall back to the root WebApplicationContext. + *

As of 6.2, this will work within a DispatcherServlet request as well as with the root + * WebApplicationContext (outside a DispatcherServlet). * @param request current HTTP request * @see org.springframework.web.servlet.DispatcherServlet * @see #RequestContext(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletContext) */ public RequestContext(HttpServletRequest request) { - this(request, null, null, null); + this(request, null, request.getServletContext(), null); } /** * Create a new RequestContext for the given request, using the request attributes for Errors retrieval. *

This only works with InternalResourceViews, as Errors instances are part of the model and not * normally exposed as request attributes. It will typically be used within JSPs or custom tags. - *

Will only work within a DispatcherServlet request. - * Pass in a ServletContext to be able to fall back to the root WebApplicationContext. + *

As of 6.2, this will work within a DispatcherServlet request as well as with the root + * WebApplicationContext (outside a DispatcherServlet). * @param request current HTTP request * @param response current HTTP response * @see org.springframework.web.servlet.DispatcherServlet * @see #RequestContext(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.ServletContext, Map) */ public RequestContext(HttpServletRequest request, HttpServletResponse response) { - this(request, response, null, null); + this(request, response, request.getServletContext(), null); } /** @@ -176,8 +176,8 @@ public class RequestContext { /** * Create a new RequestContext for the given request, using the given model attributes for Errors retrieval. *

This works with all View implementations. It will typically be used by View implementations. - *

Will only work within a DispatcherServlet request. - * Pass in a ServletContext to be able to fall back to the root WebApplicationContext. + *

As of 6.2, this will work within a DispatcherServlet request as well as with the root + * WebApplicationContext (outside a DispatcherServlet). * @param request current HTTP request * @param model the model attributes for the current view (can be {@code null}, * using the request attributes for Errors retrieval) @@ -185,7 +185,7 @@ public class RequestContext { * @see #RequestContext(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.ServletContext, Map) */ public RequestContext(HttpServletRequest request, @Nullable Map model) { - this(request, null, null, model); + this(request, null, request.getServletContext(), model); } /** @@ -281,13 +281,6 @@ public class RequestContext { return this.webApplicationContext; } - /** - * Return the current WebApplicationContext as MessageSource. - */ - public final MessageSource getMessageSource() { - return this.webApplicationContext; - } - /** * Return the model Map that this RequestContext encapsulates, if any. * @return the populated model Map, or {@code null} if none available @@ -297,13 +290,22 @@ public class RequestContext { return this.model; } + /** + * Return the MessageSource to use (typically the current WebApplicationContext). + *

Note: As of 6.2, this method is non-final and therefore overridable. + */ + public MessageSource getMessageSource() { + return this.webApplicationContext; + } + /** * Return the current Locale (falling back to the request locale; never {@code null}). *

Typically coming from a DispatcherServlet's {@link LocaleResolver}. * Also includes a fallback check for JSTL's Locale attribute. + *

Note: As of 6.2, this method is non-final and therefore overridable. * @see RequestContextUtils#getLocale */ - public final Locale getLocale() { + public Locale getLocale() { return (this.locale != null ? this.locale : getFallbackLocale()); }