Class RequestContextUtils
DispatcherServlet
.
Supports lookup of current WebApplicationContext, LocaleResolver, Locale, ThemeResolver, Theme, and MultipartResolver.
- Since:
- 03.03.2003
- Author:
- Juergen Hoeller, Rossen Stoyanchev
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The name of the bean to use to determine if an implementation ofRequestDataValueProcessor
has been configured. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic WebApplicationContext
Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request.static WebApplicationContext
findWebApplicationContext
(HttpServletRequest request, ServletContext servletContext) Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request.static FlashMapManager
getFlashMapManager
(HttpServletRequest request) Return theFlashMapManager
instance to save flash attributes.getInputFlashMap
(HttpServletRequest request) Return read-only "input" flash attributes from request before redirect.static Locale
getLocale
(HttpServletRequest request) Retrieve the current locale from the given request, using theLocaleResolver
bound to the request by theDispatcherServlet
(if available), falling back to the request's locale based on theAccept-Language
header or the default locale for the server.static LocaleResolver
getLocaleResolver
(HttpServletRequest request) Return theLocaleResolver
that has been bound to the request by theDispatcherServlet
.static FlashMap
getOutputFlashMap
(HttpServletRequest request) Return "output" FlashMap to save attributes for request after redirect.static Theme
getTheme
(HttpServletRequest request) Deprecated.as of 6.0, with no direct replacementstatic ThemeResolver
getThemeResolver
(HttpServletRequest request) Deprecated.as of 6.0, with no direct replacementstatic ThemeSource
getThemeSource
(HttpServletRequest request) Deprecated.as of 6.0, with no direct replacementstatic TimeZone
getTimeZone
(HttpServletRequest request) Retrieve the current time zone from the given request, using theTimeZoneAwareLocaleContext
in theLocaleResolver
bound to the request by theDispatcherServlet
(if available).static void
saveOutputFlashMap
(String location, HttpServletRequest request, HttpServletResponse response) Convenience method that retrieves the"output" FlashMap
, updates it with the path and query params of the target URL, and then saves it using theFlashMapManager
.
-
Field Details
-
REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
The name of the bean to use to determine if an implementation ofRequestDataValueProcessor
has been configured.- Since:
- 4.2.1
- See Also:
-
-
Constructor Details
-
RequestContextUtils
public RequestContextUtils()
-
-
Method Details
-
findWebApplicationContext
@Nullable public static WebApplicationContext findWebApplicationContext(HttpServletRequest request, @Nullable ServletContext servletContext) Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request. The global context will be found via the ServletContext or via ContextLoader's current context.NOTE: This variant remains compatible with Servlet 2.5, explicitly checking a given ServletContext instead of deriving it from the request.
- Parameters:
request
- current HTTP requestservletContext
- current servlet context- Returns:
- the request-specific WebApplicationContext, or the global one
if no request-specific context has been found, or
null
if none - Since:
- 4.2.1
- See Also:
-
findWebApplicationContext
Look for the WebApplicationContext associated with the DispatcherServlet that has initiated request processing, and for the global context if none was found associated with the current request. The global context will be found via the ServletContext or via ContextLoader's current context.- Parameters:
request
- current HTTP request- Returns:
- the request-specific WebApplicationContext, or the global one
if no request-specific context has been found, or
null
if none - Since:
- 4.2.1
- See Also:
-
getLocaleResolver
Return theLocaleResolver
that has been bound to the request by theDispatcherServlet
.- Parameters:
request
- current HTTP request- Returns:
- the current
LocaleResolver
, ornull
if not found
-
getLocale
Retrieve the current locale from the given request, using theLocaleResolver
bound to the request by theDispatcherServlet
(if available), falling back to the request's locale based on theAccept-Language
header or the default locale for the server.This method serves as a straightforward alternative to the standard Servlet
ServletRequest.getLocale()
method, falling back to the latter if no more specific locale has been found.Consider using
LocaleContextHolder.getLocale()
which will normally be populated with the same Locale.- Parameters:
request
- current HTTP request- Returns:
- the current locale for the given request, either from the LocaleResolver or from the plain request itself
- See Also:
-
getTimeZone
Retrieve the current time zone from the given request, using theTimeZoneAwareLocaleContext
in theLocaleResolver
bound to the request by theDispatcherServlet
(if available).Note: This method returns
null
if no specific time zone can be resolved for the given request. This is in contrast togetLocale(jakarta.servlet.http.HttpServletRequest)
where it is always possible to fall back to the request's locale based on theAccept-Language
header or the default locale for the server.Consider using
LocaleContextHolder.getTimeZone()
which will normally be populated with the sameTimeZone
: that method only differs in terms of its fallback to the system time zone if theLocaleResolver
hasn't provided a specific time zone (instead of this method'snull
).- Parameters:
request
- current HTTP request- Returns:
- the current time zone for the given request, either from the
TimeZoneAwareLocaleContext
ornull
if none associated - See Also:
-
getThemeResolver
Deprecated.as of 6.0, with no direct replacementReturn the ThemeResolver that has been bound to the request by the DispatcherServlet.- Parameters:
request
- current HTTP request- Returns:
- the current ThemeResolver, or
null
if not found
-
getThemeSource
Deprecated.as of 6.0, with no direct replacementReturn the ThemeSource that has been bound to the request by the DispatcherServlet.- Parameters:
request
- current HTTP request- Returns:
- the current ThemeSource
-
getTheme
Deprecated.as of 6.0, with no direct replacementRetrieve the current theme from the given request, using the ThemeResolver and ThemeSource bound to the request by the DispatcherServlet.- Parameters:
request
- current HTTP request- Returns:
- the current theme, or
null
if not found - See Also:
-
getInputFlashMap
Return read-only "input" flash attributes from request before redirect.- Parameters:
request
- current request- Returns:
- a read-only Map, or
null
if not found - See Also:
-
getOutputFlashMap
Return "output" FlashMap to save attributes for request after redirect.- Parameters:
request
- current request- Returns:
- a
FlashMap
instance, nevernull
within aDispatcherServlet
-handled request
-
getFlashMapManager
Return theFlashMapManager
instance to save flash attributes.As of 5.0 the convenience method
saveOutputFlashMap(java.lang.String, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)
may be used to save the "output" FlashMap.- Parameters:
request
- the current request- Returns:
- a
FlashMapManager
instance, nevernull
within aDispatcherServlet
-handled request
-
saveOutputFlashMap
public static void saveOutputFlashMap(String location, HttpServletRequest request, HttpServletResponse response) Convenience method that retrieves the"output" FlashMap
, updates it with the path and query params of the target URL, and then saves it using theFlashMapManager
.- Parameters:
location
- the target URL for the redirectrequest
- the current requestresponse
- the current response- Since:
- 5.0
-