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
This commit is contained in:
Juergen Hoeller 2024-06-03 15:27:33 +02:00
parent a582e48dd0
commit 34eccbe8e6
1 changed files with 19 additions and 17 deletions

View File

@ -132,29 +132,29 @@ public class RequestContext {
* Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
* <p>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.
* <p><b>Will only work within a DispatcherServlet request.</b>
* Pass in a ServletContext to be able to fall back to the root WebApplicationContext.
* <p>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.
* <p>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.
* <p><b>Will only work within a DispatcherServlet request.</b>
* Pass in a ServletContext to be able to fall back to the root WebApplicationContext.
* <p>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.
* <p>This works with all View implementations. It will typically be used by View implementations.
* <p><b>Will only work within a DispatcherServlet request.</b>
* Pass in a ServletContext to be able to fall back to the root WebApplicationContext.
* <p>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<String, Object> 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).
* <p>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}).
* <p>Typically coming from a DispatcherServlet's {@link LocaleResolver}.
* Also includes a fallback check for JSTL's Locale attribute.
* <p>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());
}