Class RequestContextUtils
DispatcherServlet.
Supports lookup of current WebApplicationContext, LocaleResolver, Locale, and MultipartResolver.
- Since:
- 03.03.2003
- Author:
- Juergen Hoeller, Rossen Stoyanchev
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe name of the bean to use to determine if an implementation ofRequestDataValueProcessorhas been configured.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic @Nullable WebApplicationContextfindWebApplicationContext(jakarta.servlet.http.HttpServletRequest request) 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 @Nullable WebApplicationContextfindWebApplicationContext(jakarta.servlet.http.HttpServletRequest request, @Nullable jakarta.servlet.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 @Nullable FlashMapManagergetFlashMapManager(jakarta.servlet.http.HttpServletRequest request) Return theFlashMapManagerinstance to save flash attributes.getInputFlashMap(jakarta.servlet.http.HttpServletRequest request) Return read-only "input" flash attributes from request before redirect.static LocalegetLocale(jakarta.servlet.http.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 @Nullable LocaleResolvergetLocaleResolver(jakarta.servlet.http.HttpServletRequest request) Return theLocaleResolverthat has been bound to the request by theDispatcherServlet.static FlashMapgetOutputFlashMap(jakarta.servlet.http.HttpServletRequest request) Return "output" FlashMap to save attributes for request after redirect.getTimeZone(jakarta.servlet.http.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, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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_NAMEThe name of the bean to use to determine if an implementation ofRequestDataValueProcessorhas been configured.- Since:
- 4.2.1
- See Also:
 
 
- 
- 
Constructor Details- 
RequestContextUtilspublic RequestContextUtils()
 
- 
- 
Method Details- 
findWebApplicationContextpublic static @Nullable WebApplicationContext findWebApplicationContext(jakarta.servlet.http.HttpServletRequest request, @Nullable jakarta.servlet.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 request
- servletContext- 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:
 
- 
findWebApplicationContextpublic static @Nullable WebApplicationContext findWebApplicationContext(jakarta.servlet.http.HttpServletRequest request) 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:
 
- 
getLocaleResolverpublic static @Nullable LocaleResolver getLocaleResolver(jakarta.servlet.http.HttpServletRequest request) Return theLocaleResolverthat has been bound to the request by theDispatcherServlet.- Parameters:
- request- current HTTP request
- Returns:
- the current LocaleResolver, ornullif not found
 
- 
getLocaleRetrieve 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:
 
- 
getTimeZoneRetrieve 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(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:
 
- 
getInputFlashMap
- 
getOutputFlashMap
- 
getFlashMapManagerpublic static @Nullable FlashMapManager getFlashMapManager(jakarta.servlet.http.HttpServletRequest request) Return theFlashMapManagerinstance to save flash attributes.As of 5.0 the convenience method saveOutputFlashMap(String, HttpServletRequest, HttpServletResponse)may be used to save the "output" FlashMap.- Parameters:
- request- the current request
- Returns:
- a FlashMapManagerinstance, nevernullwithin aDispatcherServlet-handled request
 
- 
saveOutputFlashMappublic static void saveOutputFlashMap(String location, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 redirect
- request- the current request
- response- the current response
- Since:
- 5.0
 
 
-