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 look up in 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 the LocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the request's accept-header Locale.static LocaleResolvergetLocaleResolver(HttpServletRequest request) Return the LocaleResolver that has been bound to the request by the DispatcherServlet.static FlashMapgetOutputFlashMap(HttpServletRequest request) Return "output" FlashMap to save attributes for request after redirect.static ThemegetTheme(HttpServletRequest request) Retrieves the current theme from the given request, using the ThemeResolver and ThemeSource bound to the request by the DispatcherServlet.static ThemeResolvergetThemeResolver(HttpServletRequest request) Return the ThemeResolver that has been bound to the request by the DispatcherServlet.static ThemeSourcegetThemeSource(HttpServletRequest request) Return the ThemeSource that has been bound to the request by the DispatcherServlet.static TimeZonegetTimeZone(HttpServletRequest request) Retrieve the current time zone from the given request, using the TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the system's default time zone.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 look up in 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 the LocaleResolver that has been bound to the request by the DispatcherServlet.- Parameters:
request- current HTTP request- Returns:
- the current LocaleResolver, or
nullif not found
-
getLocale
Retrieve the current locale from the given request, using the LocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the request's accept-header Locale.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 the TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the system's default time zone.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 there is always the request's accept-header locale to fall back to.Consider using
LocaleContextHolder.getTimeZone()which will normally be populated with the same TimeZone: That method only differs in terms of its fallback to the system time zone if the LocaleResolver 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
TimeZoneAwareLocaleResolver or
nullif none associated - See Also:
-
getThemeResolver
Return 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
Return the ThemeSource that has been bound to the request by the DispatcherServlet.- Parameters:
request- current HTTP request- Returns:
- the current ThemeSource
-
getTheme
Retrieves 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
-