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

public Class RequestContext

extends Object
Class Inheritance
Known Direct Subclasses
org.springframework.web.servlet.support.JspAwareRequestContext
Imports
java.util.HashMap, .List, .Locale, .Map, .TimeZone, jakarta.servlet.ServletContext, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, .HttpSession, jakarta.servlet.jsp.jstl.core.Config, org.springframework.context.MessageSource, .MessageSourceResolvable, .NoSuchMessageException, org.springframework.context.i18n.LocaleContext, .SimpleTimeZoneAwareLocaleContext, .TimeZoneAwareLocaleContext, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .StringUtils, org.springframework.validation.BindException, .BindingResult, .Errors, org.springframework.web.bind.EscapedErrors, org.springframework.web.context.WebApplicationContext, org.springframework.web.servlet.LocaleContextResolver, .LocaleResolver, org.springframework.web.util.HtmlUtils, .UriComponentsBuilder, .UrlPathHelper, .WebUtils

Context holder for request-specific state, like current web application context, current locale, current theme, and potential binding errors. Provides easy access to localized messages and Errors instances.

Suitable for exposition to views, and usage within JSP's "useBean" tag, JSP scriptlets, JSTL EL, etc. Necessary for views that do not have access to the servlet request, like FreeMarker templates.

Can be instantiated manually, or automatically exposed to views as model attribute via AbstractView's "requestContextAttribute" property.

Will also work outside DispatcherServlet requests, accessing the root WebApplicationContext and using an appropriate fallback for the locale (the HttpServletRequest's primary locale).

Authors
Juergen Hoeller, Rossen Stoyanchev
Since
03.03.2003
See Also
org.springframework.web.servlet.DispatcherServlet, org.springframework.web.servlet.view.AbstractView#setRequestContextAttribute, org.springframework.web.servlet.view.UrlBasedViewResolver#setRequestContextAttribute

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
RequestContext.JstlLocaleResolver

Inner class that isolates the JSTL dependency.

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_THEME_NAME

Deprecated since 6.0. as of 6.0, with no direct replacement
Default theme name used if the RequestContext cannot find a ThemeResolver.
private Boolean
private Map<String, Errors>
protected static final boolean
private Locale
private final Map<String, Object>
private final HttpServletRequest
private RequestDataValueProcessor
private final HttpServletResponse
private final Boolean
private Theme
theme

Deprecated
private TimeZone
private UrlPathHelper
public static final String
WEB_APPLICATION_CONTEXT_ATTRIBUTE

Request attribute to hold the current web application context for RequestContext usage.

private final WebApplicationContext

Constructor Summary

AccessConstructor and Description
public
RequestContext(HttpServletRequest
current HTTP request
request
)

Create a new RequestContext for the given request, using the request attributes for Errors retrieval.

public
RequestContext(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Create a new RequestContext for the given request, using the request attributes for Errors retrieval.

public
RequestContext(HttpServletRequest
current HTTP request
request
,
ServletContext
the servlet context of the web application (can be null; necessary for fallback to root WebApplicationContext)
servletContext
)

Create a new RequestContext for the given request, using the request attributes for Errors retrieval.

public
RequestContext(HttpServletRequest
current HTTP request
request
,
Map<String, Object>
the model attributes for the current view (can be null, using the request attributes for Errors retrieval)
model
)

Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.

public
RequestContext(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
ServletContext
the servlet context of the web application (can be null; necessary for fallback to root WebApplicationContext)
servletContext
,
Map<String, Object>
the model attributes for the current view (can be null, using the request attributes for Errors retrieval)
model
)

Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.

Method Summary

Modifier and TypeMethod and Description
public void
changeLocale(Locale
the new locale
locale
)

Change the current locale to the specified one, storing the new locale through the configured LocaleResolver.

public void
changeLocale(Locale
the new locale
locale
,
TimeZone
the new time zone
timeZone
)

Change the current locale to the specified locale and time zone context, storing the new locale context through the configured LocaleResolver.

public void
changeTheme(Theme
the new theme
theme
)
Deprecated since 6.0. as of 6.0, with no direct replacement

Change the current theme to the specified one, storing the new theme name through the configured ThemeResolver.

public void
changeTheme(String
the name of the new theme
themeName
)
Deprecated as of 6.0, with no direct replacement

Change the current theme to the specified theme by name, storing the new theme name through the configured ThemeResolver.

public BindStatus

Returns:

the new BindStatus instance
getBindStatus
(String
the bean and property path for which values and errors will be resolved (e.g. "person.age")
path
)

Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.

public BindStatus

Returns:

the new BindStatus instance
getBindStatus
(String
the bean and property path for which values and errors will be resolved (e.g. "person.age")
path
,
boolean
create a BindStatus with automatic HTML escaping?
htmlEscape
)

Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.

public String
getContextPath()

Return the context path of the original request, that is, the path that indicates the current web application.

public String

Returns:

a URL that points back to the server with an absolute path (also URL-encoded accordingly)
getContextUrl
(String
the relative URL part
relativeUrl
)

Return a context-aware URl for the given relative URL.

public String

Returns:

a URL that points back to the server with an absolute path (also URL-encoded accordingly)
getContextUrl
(String
the relative URL part
relativeUrl
,
Map<String, ?>
a map of parameters to insert as placeholders in the url
params
)

Return a context-aware URl for the given relative URL with placeholders (named keys with braces {}).

public Boolean

Returns:

whether default HTML escaping is enabled (null = no explicit default)
getDefaultHtmlEscape
()

Return the default HTML escape setting, differentiating between no default specified and an explicit value.

public Errors

Returns:

the Errors instance, or null if not found
getErrors
(String
the name of the bind object
name
)

Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.

public Errors

Returns:

the Errors instance, or null if not found
getErrors
(String
the name of the bind object
name
,
boolean
create an Errors instance with automatic HTML escaping?
htmlEscape
)

Retrieve the Errors instance for the given bind object.

protected Locale

Returns:

the fallback locale (never null)
getFallbackLocale
()

Determine the fallback locale for this context.

protected Theme

Returns:

the fallback theme (never null)
getFallbackTheme
()
Deprecated as of 6.0, with no direct replacement

Determine the fallback theme for this context.

protected TimeZone

Returns:

the fallback time zone (or null if none derivable from the request)
getFallbackTimeZone
()

Determine the fallback time zone for this context.

public final Locale
getLocale()

Return the current Locale (falling back to the request locale; never null).

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
List<?>
arguments for the message as a List, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
,
boolean
if the message should be HTML-escaped
htmlEscape
)

Retrieve the message for the given code.

public String

Returns:

the message
getMessage
(String
the code of the message
code
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
List<?>
arguments for the message as a List, or null if none
args
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
boolean
if the message should be HTML-escaped
htmlEscape
)

Retrieve the message for the given code.

public String

Returns:

the message
getMessage
(MessageSourceResolvable
the MessageSourceResolvable
resolvable
)

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(MessageSourceResolvable
the MessageSourceResolvable
resolvable
,
boolean
if the message should be HTML-escaped
htmlEscape
)

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).

public final MessageSource
getMessageSource()

Return the current WebApplicationContext as MessageSource.

public final Map<String, Object>

Returns:

the populated model Map, or null if none available
getModel
()

Return the model Map that this RequestContext encapsulates, if any.

protected Object

Returns:

the model object
getModelObject
(String
the name of the model object
modelName
)

Retrieve the model object for the given model name, either from the model or from the request attributes.

public String
getPathToServlet()

Return the path to URL mappings within the current servlet including the context path and the servlet path of the original request.

public String
getQueryString()

Return the query string of the current request, that is, the part after the request path.

protected final HttpServletRequest
getRequest()

Return the underlying HttpServletRequest.

public RequestDataValueProcessor
getRequestDataValueProcessor()

Return the RequestDataValueProcessor instance to use obtained from the WebApplicationContext under the name "requestDataValueProcessor".

public String
getRequestUri()

Return the request URI of the original request, that is, the invoked URL without parameters.

public Boolean

Returns:

whether default use of response encoding HTML escaping is enabled (null = no explicit default)
getResponseEncodedHtmlEscape
()

Return the default setting about use of response encoding for HTML escape setting, differentiating between no default specified and an explicit value.

protected final ServletContext
getServletContext()

Return the underlying ServletContext.

public Theme
getTheme()
Deprecated since 6.0. as of 6.0, with no direct replacement

Return the current theme (never null).

public String

Returns:

the message
getThemeMessage
(String
the code of the message
code
,
String
the String to return if the lookup fails
defaultMessage
)
Deprecated as of 6.0, with no direct replacement

Retrieve the theme message for the given code.

public String

Returns:

the message
getThemeMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
)
Deprecated as of 6.0, with no direct replacement

Retrieve the theme message for the given code.

public String

Returns:

the message
getThemeMessage
(String
the code of the message
code
,
List<?>
arguments for the message as a List, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
)
Deprecated as of 6.0, with no direct replacement

Retrieve the theme message for the given code.

public String

Returns:

the message
getThemeMessage
(String
the code of the message
code
)
Deprecated as of 6.0, with no direct replacement

Retrieve the theme message for the given code.

public String

Returns:

the message
getThemeMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
)
Deprecated as of 6.0, with no direct replacement

Retrieve the theme message for the given code.

public String

Returns:

the message
getThemeMessage
(String
the code of the message
code
,
List<?>
arguments for the message as a List, or null if none
args
)
Deprecated as of 6.0, with no direct replacement

Retrieve the theme message for the given code.

public String

Returns:

the message
getThemeMessage
(MessageSourceResolvable
the MessageSourceResolvable
resolvable
)
Deprecated as of 6.0, with no direct replacement

Retrieve the given MessageSourceResolvable in the current theme.

public TimeZone
getTimeZone()

Return the current TimeZone (or null if none derivable from the request).

public UrlPathHelper
getUrlPathHelper()

Return the UrlPathHelper used for context path and request URI decoding.

public final WebApplicationContext
getWebApplicationContext()

Return the current WebApplicationContext.

public boolean
isDefaultHtmlEscape()

Is default HTML escaping active?

public boolean
isResponseEncodedHtmlEscape()

Is HTML escaping using the response encoding by default?

public void
setDefaultHtmlEscape(boolean defaultHtmlEscape)

(De)activate default HTML escaping for messages and errors, for the scope of this RequestContext.

public void
setUrlPathHelper(UrlPathHelper urlPathHelper)

Set the UrlPathHelper to use for context path and request URI decoding.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait