Top Description Fields Constructors Methods
org.springframework.web.servlet.support

public abstract Class RequestContextUtils

extends Object
Class Inheritance
Imports
java.util.Locale, .Map, .TimeZone, jakarta.servlet.ServletContext, .ServletRequest, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.context.i18n.LocaleContext, .TimeZoneAwareLocaleContext, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, org.springframework.web.context.ContextLoader, .WebApplicationContext, org.springframework.web.context.support.WebApplicationContextUtils, org.springframework.web.servlet.DispatcherServlet, .FlashMap, .FlashMapManager, .LocaleContextResolver, .LocaleResolver, org.springframework.web.util.UriComponents, .UriComponentsBuilder

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.

Authors
Juergen Hoeller, Rossen Stoyanchev
Since
03.03.2003
See Also
RequestContext, org.springframework.web.servlet.DispatcherServlet

Field Summary

Modifier and TypeField and Description
public static final String
REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME

The name of the bean to use to determine if an implementation of RequestDataValueProcessor has been configured.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static WebApplicationContext

Returns:

the request-specific WebApplicationContext, or the global one if no request-specific context has been found, or null if none
findWebApplicationContext
(HttpServletRequest
current HTTP request
request
,
ServletContext
current servlet context
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.

public static WebApplicationContext

Returns:

the request-specific WebApplicationContext, or the global one if no request-specific context has been found, or null if none
findWebApplicationContext
(HttpServletRequest
current HTTP request
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.

public static FlashMapManager

Returns:

a FlashMapManager instance, never null within a DispatcherServlet-handled request
getFlashMapManager
(HttpServletRequest
the current request
request
)

Return the FlashMapManager instance to save flash attributes.

public static Map<String, ?>

Returns:

a read-only Map, or null if not found
getInputFlashMap
(HttpServletRequest
current request
request
)

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

public static Locale

Returns:

the current locale for the given request, either from the LocaleResolver or from the plain request itself
getLocale
(HttpServletRequest
current HTTP request
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 locale based on the Accept-Language header or the default locale for the server.

public static LocaleResolver

Returns:

the current LocaleResolver, or null if not found
getLocaleResolver
(HttpServletRequest
current HTTP request
request
)

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

public static FlashMap

Returns:

a FlashMap instance, never null within a DispatcherServlet-handled request
getOutputFlashMap
(HttpServletRequest
current request
request
)

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

public static Theme

Returns:

the current theme, or null if not found
getTheme
(HttpServletRequest
current HTTP request
request
)
Deprecated since 6.0. as of 6.0, with no direct replacement

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

public static ThemeResolver

Returns:

the current ThemeResolver, or null if not found
getThemeResolver
(HttpServletRequest
current HTTP request
request
)
Deprecated since 6.0. as of 6.0, with no direct replacement

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

public static ThemeSource

Returns:

the current ThemeSource
getThemeSource
(HttpServletRequest
current HTTP request
request
)
Deprecated since 6.0. as of 6.0, with no direct replacement

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

public static TimeZone

Returns:

the current time zone for the given request, either from the TimeZoneAwareLocaleContext or null if none associated
getTimeZone
(HttpServletRequest
current HTTP request
request
)

Retrieve the current time zone from the given request, using the TimeZoneAwareLocaleContext in the LocaleResolver bound to the request by the DispatcherServlet (if available).

public static void
saveOutputFlashMap(String
the target URL for the redirect
location
,
HttpServletRequest
the current request
request
,
HttpServletResponse
the current response
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAMEback to summary
public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME

The name of the bean to use to determine if an implementation of RequestDataValueProcessor has been configured.

Since
4.2.1

Constructor Detail

RequestContextUtilsback to summary
public RequestContextUtils()

Method Detail

findWebApplicationContextback to summary
public 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. 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:HttpServletRequest

current HTTP request

servletContext:ServletContext

current servlet context

Returns:WebApplicationContext

the request-specific WebApplicationContext, or the global one if no request-specific context has been found, or null if none

Annotations
@Nullable
Since
4.2.1
See Also
DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE, WebApplicationContextUtils#getWebApplicationContext(ServletContext), ContextLoader#getCurrentWebApplicationContext()
findWebApplicationContextback to summary
public 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. The global context will be found via the ServletContext or via ContextLoader's current context.

Parameters
request:HttpServletRequest

current HTTP request

Returns:WebApplicationContext

the request-specific WebApplicationContext, or the global one if no request-specific context has been found, or null if none

Annotations
@Nullable
Since
4.2.1
See Also
findWebApplicationContext(HttpServletRequest, ServletContext), ServletRequest#getServletContext(), ContextLoader#getCurrentWebApplicationContext()
getFlashMapManagerback to summary
public static FlashMapManager getFlashMapManager(HttpServletRequest request)

Return the FlashMapManager instance to save flash attributes.

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

Parameters
request:HttpServletRequest

the current request

Returns:FlashMapManager

a FlashMapManager instance, never null within a DispatcherServlet-handled request

Annotations
@Nullable
getInputFlashMapback to summary
public static Map<String, ?> getInputFlashMap(HttpServletRequest request)

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

Parameters
request:HttpServletRequest

current request

Returns:Map<String, ?>

a read-only Map, or null if not found

Annotations
@SuppressWarnings:unchecked
@Nullable
See Also
FlashMap
getLocaleback to summary
public 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 locale based on the Accept-Language header or the default locale for the server.

This method serves as a straightforward alternative to the standard Servlet jakarta.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.

Parameters
request:HttpServletRequest

current HTTP request

Returns:Locale

the current locale for the given request, either from the LocaleResolver or from the plain request itself

See Also
getLocaleResolver, org.springframework.context.i18n.LocaleContextHolder#getLocale()
getLocaleResolverback to summary
public static LocaleResolver getLocaleResolver(HttpServletRequest request)

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

Parameters
request:HttpServletRequest

current HTTP request

Returns:LocaleResolver

the current LocaleResolver, or null if not found

Annotations
@Nullable
getOutputFlashMapback to summary
public static FlashMap getOutputFlashMap(HttpServletRequest request)

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

Parameters
request:HttpServletRequest

current request

Returns:FlashMap

a FlashMap instance, never null within a DispatcherServlet-handled request

getThemeback to summary
public static Theme getTheme(HttpServletRequest request)

Deprecated

since 6.0.

as of 6.0, with no direct replacement

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

Parameters
request:HttpServletRequest

current HTTP request

Returns:Theme

the current theme, or null if not found

Annotations
@Nullable
@Deprecated
since:6.0
See Also
getThemeResolver
getThemeResolverback to summary
public static ThemeResolver getThemeResolver(HttpServletRequest request)

Deprecated

since 6.0.

as of 6.0, with no direct replacement

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

Parameters
request:HttpServletRequest

current HTTP request

Returns:ThemeResolver

the current ThemeResolver, or null if not found

Annotations
@Nullable
@Deprecated
since:6.0
getThemeSourceback to summary
public static ThemeSource getThemeSource(HttpServletRequest request)

Deprecated

since 6.0.

as of 6.0, with no direct replacement

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

Parameters
request:HttpServletRequest

current HTTP request

Returns:ThemeSource

the current ThemeSource

Annotations
@Nullable
@Deprecated
since:6.0
getTimeZoneback to summary
public static TimeZone getTimeZone(HttpServletRequest request)

Retrieve the current time zone from the given request, using the TimeZoneAwareLocaleContext in the LocaleResolver bound to the request by the DispatcherServlet (if available).

Note

This method returns null if no specific time zone can be resolved for the given request. This is in contrast to getLocale where it is always possible to fall back to the request's locale based on the Accept-Language header or the default locale for the server.

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).

Parameters
request:HttpServletRequest

current HTTP request

Returns:TimeZone

the current time zone for the given request, either from the TimeZoneAwareLocaleContext or null if none associated

Annotations
@Nullable
See Also
getLocaleResolver, org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
saveOutputFlashMapback to summary
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 the FlashMapManager.

Parameters
location:String

the target URL for the redirect

request:HttpServletRequest

the current request

response:HttpServletResponse

the current response

Since
5.0