spring-framework / org.springframework.web.servlet.support / RequestContextUtils

RequestContextUtils

abstract class RequestContextUtils

Utility class for easy access to request-specific state which has been set by the org.springframework.web.servlet.DispatcherServlet.

Supports lookup of current WebApplicationContext, LocaleResolver, Locale, ThemeResolver, Theme, and MultipartResolver.

Author
Juergen Hoeller

Author
Rossen Stoyanchev

Since
03.03.2003

See Also
RequestContextorg.springframework.web.servlet.DispatcherServlet

Constructors

<init>

RequestContextUtils()

Utility class for easy access to request-specific state which has been set by the org.springframework.web.servlet.DispatcherServlet.

Supports lookup of current WebApplicationContext, LocaleResolver, Locale, ThemeResolver, Theme, and MultipartResolver.

Properties

REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME

static val REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME: String

The name of the bean to use to look up in an implementation of RequestDataValueProcessor has been configured.

Functions

findWebApplicationContext

open static fun findWebApplicationContext(request: HttpServletRequest, servletContext: ServletContext): 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. 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.

open static fun findWebApplicationContext(request: HttpServletRequest): 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. The global context will be found via the ServletContext or via ContextLoader's current context.

NOTE: This variant requires Servlet 3.0+ and is generally recommended for forward-looking custom user code.

getFlashMapManager

open static fun getFlashMapManager(request: HttpServletRequest): FlashMapManager

Return the FlashMapManager instance to save flash attributes.

As of 5.0 the convenience method #saveOutputFlashMap may be used to save the "output" FlashMap.

getInputFlashMap

open static fun getInputFlashMap(request: HttpServletRequest): MutableMap<String, *>

Return read-only "input" flash attributes from request before redirect.

getLocale

open static fun getLocale(request: HttpServletRequest): Locale

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 javax.servlet.http.HttpServletRequest#getLocale() method, falling back to the latter if no more specific locale has been found.

Consider using org.springframework.context.i18n.LocaleContextHolder#getLocale() which will normally be populated with the same Locale.

getLocaleResolver

open static fun getLocaleResolver(request: HttpServletRequest): LocaleResolver

Return the LocaleResolver that has been bound to the request by the DispatcherServlet.

getOutputFlashMap

open static fun getOutputFlashMap(request: HttpServletRequest): FlashMap

Return "output" FlashMap to save attributes for request after redirect.

getTheme

open static fun getTheme(request: HttpServletRequest): Theme

Retrieves the current theme from the given request, using the ThemeResolver and ThemeSource bound to the request by the DispatcherServlet.

getThemeResolver

open static fun getThemeResolver(request: HttpServletRequest): ThemeResolver

Return the ThemeResolver that has been bound to the request by the DispatcherServlet.

getThemeSource

open static fun getThemeSource(request: HttpServletRequest): ThemeSource

Return the ThemeSource that has been bound to the request by the DispatcherServlet.

getTimeZone

open static fun getTimeZone(request: HttpServletRequest): TimeZone

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 null if no specific time zone can be resolved for the given request. This is in contrast to #getLocale where there is always the request's accept-header locale to fall back to.

Consider using org.springframework.context.i18n.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).

saveOutputFlashMap

open static fun saveOutputFlashMap(location: String, request: HttpServletRequest, response: HttpServletResponse): Unit

Convenience method that retrieves the "output", updates it with the path and query params of the target URL, and then saves it using the FlashMapManager.