public abstract class RequestContextUtils extends Object
DispatcherServlet
.
Supports lookup of current WebApplicationContext, LocaleResolver, Locale, ThemeResolver, Theme, and MultipartResolver.
RequestContext
,
DispatcherServlet
Modifier and Type | Field and Description |
---|---|
static String |
REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
The name of the bean to use to look up in an implementation of
RequestDataValueProcessor has been configured. |
Constructor and Description |
---|
RequestContextUtils() |
Modifier and Type | Method and Description |
---|---|
static WebApplicationContext |
findWebApplicationContext(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 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 the
FlashMapManager instance to save flash attributes. |
static Map<String,?> |
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 the
LocaleResolver bound to the request by the DispatcherServlet
(if available), falling back to the request's accept-header Locale.
|
static LocaleResolver |
getLocaleResolver(HttpServletRequest request)
Return the LocaleResolver that has been bound to the request by the
DispatcherServlet.
|
static FlashMap |
getOutputFlashMap(HttpServletRequest request)
Return "output" FlashMap to save attributes for request after redirect.
|
static Theme |
getTheme(HttpServletRequest request)
Retrieves the current theme from the given request, using the ThemeResolver
and ThemeSource bound to the request by the DispatcherServlet.
|
static ThemeResolver |
getThemeResolver(HttpServletRequest request)
Return the ThemeResolver that has been bound to the request by the
DispatcherServlet.
|
static ThemeSource |
getThemeSource(HttpServletRequest request)
Return the ThemeSource that has been bound to the request by the
DispatcherServlet.
|
static TimeZone |
getTimeZone(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 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 the FlashMapManager . |
public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
RequestDataValueProcessor
has been configured.@Nullable public static WebApplicationContext findWebApplicationContext(HttpServletRequest request, @Nullable ServletContext servletContext)
NOTE: This variant remains compatible with Servlet 2.5, explicitly checking a given ServletContext instead of deriving it from the request.
request
- current HTTP requestservletContext
- current servlet contextnull
if noneDispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE
,
WebApplicationContextUtils.getWebApplicationContext(ServletContext)
,
ContextLoader.getCurrentWebApplicationContext()
@Nullable public static WebApplicationContext findWebApplicationContext(HttpServletRequest request)
NOTE: This variant requires Servlet 3.0+ and is generally recommended for forward-looking custom user code.
request
- current HTTP requestnull
if nonefindWebApplicationContext(HttpServletRequest, ServletContext)
,
ServletRequest.getServletContext()
,
ContextLoader.getCurrentWebApplicationContext()
@Nullable public static LocaleResolver getLocaleResolver(HttpServletRequest request)
request
- current HTTP requestnull
if not foundpublic static Locale getLocale(HttpServletRequest request)
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.
request
- current HTTP requestgetLocaleResolver(javax.servlet.http.HttpServletRequest)
,
LocaleContextHolder.getLocale()
@Nullable public static TimeZone getTimeZone(HttpServletRequest request)
Note: This method returns null
if no specific time zone can be
resolved for the given request. This is in contrast to getLocale(javax.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's null
).
request
- current HTTP requestnull
if none associatedgetLocaleResolver(javax.servlet.http.HttpServletRequest)
,
LocaleContextHolder.getTimeZone()
@Nullable public static ThemeResolver getThemeResolver(HttpServletRequest request)
request
- current HTTP requestnull
if not found@Nullable public static ThemeSource getThemeSource(HttpServletRequest request)
request
- current HTTP request@Nullable public static Theme getTheme(HttpServletRequest request)
request
- current HTTP requestnull
if not foundgetThemeResolver(javax.servlet.http.HttpServletRequest)
@Nullable public static Map<String,?> getInputFlashMap(HttpServletRequest request)
request
- current requestnull
if not foundFlashMap
public static FlashMap getOutputFlashMap(HttpServletRequest request)
request
- current requestFlashMap
instance, never null
within a
DispatcherServlet
-handled request@Nullable public static FlashMapManager getFlashMapManager(HttpServletRequest request)
FlashMapManager
instance to save flash attributes.
As of 5.0 the convenience method saveOutputFlashMap(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
may be
used to save the "output" FlashMap.
request
- the current requestFlashMapManager
instance, never null
within a
DispatcherServlet
-handled requestpublic static void saveOutputFlashMap(String location, HttpServletRequest request, HttpServletResponse response)
"output"
FlashMap
, updates it with the path and query params of the target URL,
and then saves it using the FlashMapManager
.location
- the target URL for the redirectrequest
- the current requestresponse
- the current response