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 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());
}