Class RequestContextUtils

java.lang.Object
org.springframework.web.servlet.support.RequestContextUtils

public abstract class RequestContextUtils extends Object
Utility class for easy access to request-specific state which has been set by the DispatcherServlet.

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

Since:
03.03.2003
Author:
Juergen Hoeller, Rossen Stoyanchev
See Also:
  • Field Details

    • REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME

      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
      See Also:
  • Constructor Details

    • RequestContextUtils

      public RequestContextUtils()
  • Method Details

    • findWebApplicationContext

      @Nullable public static org.springframework.web.context.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 request
      servletContext - current servlet context
      Returns:
      the request-specific WebApplicationContext, or the global one if no request-specific context has been found, or null if none
      Since:
      4.2.1
      See Also:
    • findWebApplicationContext

      @Nullable public static org.springframework.web.context.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 - current HTTP request
      Returns:
      the request-specific WebApplicationContext, or the global one if no request-specific context has been found, or null if none
      Since:
      4.2.1
      See Also:
    • getLocaleResolver

      @Nullable public static LocaleResolver getLocaleResolver(HttpServletRequest request)
      Return the LocaleResolver that has been bound to the request by the DispatcherServlet.
      Parameters:
      request - current HTTP request
      Returns:
      the current LocaleResolver, or null if not found
    • getLocale

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

      @Nullable 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(jakarta.servlet.http.HttpServletRequest) 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 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 - current HTTP request
      Returns:
      the current time zone for the given request, either from the TimeZoneAwareLocaleContext or null if none associated
      See Also:
    • getThemeResolver

      @Nullable @Deprecated(since="6.0") public static ThemeResolver getThemeResolver(HttpServletRequest request)
      Deprecated.
      as of 6.0, with no direct replacement
      Return the ThemeResolver that has been bound to the request by the DispatcherServlet.
      Parameters:
      request - current HTTP request
      Returns:
      the current ThemeResolver, or null if not found
    • getThemeSource

      @Nullable @Deprecated(since="6.0") public static org.springframework.ui.context.ThemeSource getThemeSource(HttpServletRequest request)
      Deprecated.
      as of 6.0, with no direct replacement
      Return the ThemeSource that has been bound to the request by the DispatcherServlet.
      Parameters:
      request - current HTTP request
      Returns:
      the current ThemeSource
    • getTheme

      @Nullable @Deprecated(since="6.0") public static org.springframework.ui.context.Theme getTheme(HttpServletRequest request)
      Deprecated.
      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 - current HTTP request
      Returns:
      the current theme, or null if not found
      See Also:
    • getInputFlashMap

      @Nullable public static Map<String,?> getInputFlashMap(HttpServletRequest request)
      Return read-only "input" flash attributes from request before redirect.
      Parameters:
      request - current request
      Returns:
      a read-only Map, or null if not found
      See Also:
    • getOutputFlashMap

      public static FlashMap getOutputFlashMap(HttpServletRequest request)
      Return "output" FlashMap to save attributes for request after redirect.
      Parameters:
      request - current request
      Returns:
      a FlashMap instance, never null within a DispatcherServlet-handled request
    • getFlashMapManager

      @Nullable public static FlashMapManager getFlashMapManager(HttpServletRequest request)
      Return the FlashMapManager instance 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 FlashMapManager instance, never null within a DispatcherServlet-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 the FlashMapManager.
      Parameters:
      location - the target URL for the redirect
      request - the current request
      response - the current response
      Since:
      5.0