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
FieldsModifier and TypeFieldDescriptionstatic final StringThe name of the bean to use to determine if an implementation ofRequestDataValueProcessorhas been configured. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic WebApplicationContextLook 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 WebApplicationContextfindWebApplicationContext(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 FlashMapManagergetFlashMapManager(HttpServletRequest request) Return theFlashMapManagerinstance to save flash attributes.getInputFlashMap(HttpServletRequest request) Return read-only "input" flash attributes from request before redirect.static LocalegetLocale(HttpServletRequest request) Retrieve the current locale from the given request, using theLocaleResolverbound to the request by theDispatcherServlet(if available), falling back to the request's locale based on theAccept-Languageheader or the default locale for the server.static LocaleResolvergetLocaleResolver(HttpServletRequest request) Return theLocaleResolverthat has been bound to the request by theDispatcherServlet.static FlashMapgetOutputFlashMap(HttpServletRequest request) Return "output" FlashMap to save attributes for request after redirect.static ThemegetTheme(HttpServletRequest request) Deprecated.as of 6.0, with no direct replacementstatic ThemeResolvergetThemeResolver(HttpServletRequest request) Deprecated.as of 6.0, with no direct replacementstatic ThemeSourcegetThemeSource(HttpServletRequest request) Deprecated.as of 6.0, with no direct replacementstatic TimeZonegetTimeZone(HttpServletRequest request) Retrieve the current time zone from the given request, using theTimeZoneAwareLocaleContextin theLocaleResolverbound to the request by theDispatcherServlet(if available).static voidsaveOutputFlashMap(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 ofRequestDataValueProcessorhas 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
nullif 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
nullif none - Since:
- 4.2.1
- See Also:
-
getLocaleResolver
Return theLocaleResolverthat has been bound to the request by theDispatcherServlet.- Parameters:
request- current HTTP request- Returns:
- the current
LocaleResolver, ornullif not found
-
getLocale
Retrieve the current locale from the given request, using theLocaleResolverbound to the request by theDispatcherServlet(if available), falling back to the request's locale based on theAccept-Languageheader 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 theTimeZoneAwareLocaleContextin theLocaleResolverbound to the request by theDispatcherServlet(if available).Note: This method returns
nullif 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-Languageheader 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 theLocaleResolverhasn'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
TimeZoneAwareLocaleContextornullif none associated - See Also:
-
getThemeResolver
@Nullable @Deprecated(since="6.0") public static ThemeResolver getThemeResolver(HttpServletRequest request) 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
nullif not found
-
getThemeSource
@Nullable @Deprecated(since="6.0") public static ThemeSource getThemeSource(HttpServletRequest request) 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
nullif 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
nullif not found - See Also:
-
getOutputFlashMap
Return "output" FlashMap to save attributes for request after redirect.- Parameters:
request- current request- Returns:
- a
FlashMapinstance, nevernullwithin aDispatcherServlet-handled request
-
getFlashMapManager
Return theFlashMapManagerinstance 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
FlashMapManagerinstance, nevernullwithin 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
-