Class RequestContext
- Direct Known Subclasses:
JspAwareRequestContext
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).
- Since:
- 03.03.2003
- Author:
- Juergen Hoeller, Rossen Stoyanchev
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final boolean
static final String
Request attribute to hold the current web application context for RequestContext usage. -
Constructor Summary
ConstructorDescriptionRequestContext
(HttpServletRequest request) Create a new RequestContext for the given request, using the request attributes for Errors retrieval.RequestContext
(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable ServletContext servletContext, @Nullable Map<String, Object> model) Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.RequestContext
(HttpServletRequest request, @Nullable ServletContext servletContext) Create a new RequestContext for the given request, using the request attributes for Errors retrieval.RequestContext
(HttpServletRequest request, @Nullable Map<String, Object> model) Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.RequestContext
(HttpServletRequest request, HttpServletResponse response) Create a new RequestContext for the given request, using the request attributes for Errors retrieval. -
Method Summary
Modifier and TypeMethodDescriptionvoid
changeLocale
(Locale locale) Change the current locale to the specified one, storing the new locale through the configuredLocaleResolver
.void
changeLocale
(Locale locale, TimeZone timeZone) Change the current locale to the specified locale and time zone context, storing the new locale context through the configuredLocaleResolver
.getBindStatus
(String path) Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.getBindStatus
(String path, boolean htmlEscape) Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.Return the context path of the original request, that is, the path that indicates the current web application.getContextUrl
(String relativeUrl) Return a context-aware URl for the given relative URL.getContextUrl
(String relativeUrl, Map<String, ?> params) Return a context-aware URl for the given relative URL with placeholders (named keys with braces{}
).Return the default HTML escape setting, differentiating between no default specified and an explicit value.Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.Retrieve the Errors instance for the given bind object.protected Locale
Determine the fallback locale for this context.Determine the fallback time zone for this context.Return the current Locale (falling back to the request locale; nevernull
).getMessage
(String code) Retrieve the message for the given code, using the "defaultHtmlEscape" setting.getMessage
(String code, @Nullable List<?> args) Retrieve the message for the given code, using the "defaultHtmlEscape" setting.getMessage
(String code, @Nullable List<?> args, String defaultMessage) Retrieve the message for the given code, using the "defaultHtmlEscape" setting.getMessage
(String code, Object @Nullable [] args) Retrieve the message for the given code, using the "defaultHtmlEscape" setting.getMessage
(String code, Object @Nullable [] args, boolean htmlEscape) Retrieve the message for the given code.getMessage
(String code, Object @Nullable [] args, String defaultMessage) Retrieve the message for the given code, using the "defaultHtmlEscape" setting.getMessage
(String code, Object @Nullable [] args, String defaultMessage, boolean htmlEscape) Retrieve the message for the given code.getMessage
(String code, String defaultMessage) Retrieve the message for the given code, using the "defaultHtmlEscape" setting.getMessage
(MessageSourceResolvable resolvable) Retrieve the given MessageSourceResolvable (for example, an ObjectError instance), using the "defaultHtmlEscape" setting.getMessage
(MessageSourceResolvable resolvable, boolean htmlEscape) Retrieve the given MessageSourceResolvable (for example, an ObjectError instance).Return the MessageSource to use (typically the current WebApplicationContext).getModel()
Return the model Map that this RequestContext encapsulates, if any.getModelObject
(String modelName) Retrieve the model object for the given model name, either from the model or from the request attributes.Return the path to URL mappings within the current servlet including the context path and the servlet path of the original request.Return the query string of the current request, that is, the part after the request path.protected final HttpServletRequest
Return the underlying HttpServletRequest.Return the RequestDataValueProcessor instance to use obtained from the WebApplicationContext under the name"requestDataValueProcessor"
.Return the request URI of the original request, that is, the invoked URL without parameters.Return the default setting about use of response encoding for HTML escape setting, differentiating between no default specified and an explicit value.protected final @Nullable ServletContext
Return the underlying ServletContext.Return the current TimeZone (ornull
if none derivable from the request).Deprecated, for removal: This API element is subject to removal in a future version.final WebApplicationContext
Return the current WebApplicationContext.boolean
Is default HTML escaping active? Falls back tofalse
in case of no explicit default given.boolean
Is HTML escaping using the response encoding by default? If enabled, only XML markup significant characters will be escaped with UTF-* encodings.void
setDefaultHtmlEscape
(boolean defaultHtmlEscape) (De)activate default HTML escaping for messages and errors, for the scope of this RequestContext.void
setUrlPathHelper
(UrlPathHelper urlPathHelper) Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.
-
Field Details
-
WEB_APPLICATION_CONTEXT_ATTRIBUTE
Request attribute to hold the current web application context for RequestContext usage. By default, the DispatcherServlet's context (or the root context as fallback) is exposed. -
jstlPresent
protected static final boolean jstlPresent
-
-
Constructor Details
-
RequestContext
Create a new RequestContext for the given request, using the request attributes for Errors retrieval.This only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as request attributes. It will typically be used within JSPs or custom tags.
As of 6.2, this will work within a DispatcherServlet request as well as with the root WebApplicationContext (outside a DispatcherServlet).
- Parameters:
request
- current HTTP request- See Also:
-
RequestContext
Create a new RequestContext for the given request, using the request attributes for Errors retrieval.This only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as request attributes. It will typically be used within JSPs or custom tags.
As of 6.2, this will work within a DispatcherServlet request as well as with the root WebApplicationContext (outside a DispatcherServlet).
- Parameters:
request
- current HTTP requestresponse
- current HTTP response- See Also:
-
RequestContext
Create a new RequestContext for the given request, using the request attributes for Errors retrieval.This only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as request attributes. It will typically be used within JSPs or custom tags.
If a ServletContext is specified, the RequestContext will also work with the root WebApplicationContext (outside a DispatcherServlet).
- Parameters:
request
- current HTTP requestservletContext
- the servlet context of the web application (can benull
; necessary for fallback to root WebApplicationContext)- See Also:
-
RequestContext
Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.This works with all View implementations. It will typically be used by View implementations.
As of 6.2, this will work within a DispatcherServlet request as well as with the root WebApplicationContext (outside a DispatcherServlet).
- Parameters:
request
- current HTTP requestmodel
- the model attributes for the current view (can benull
, using the request attributes for Errors retrieval)- See Also:
-
RequestContext
public RequestContext(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable ServletContext servletContext, @Nullable Map<String, Object> model) Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.This works with all View implementations. It will typically be used by View implementations.
If a ServletContext is specified, the RequestContext will also work with a root WebApplicationContext (outside a DispatcherServlet).
- Parameters:
request
- current HTTP requestresponse
- current HTTP responseservletContext
- the servlet context of the web application (can benull
; necessary for fallback to root WebApplicationContext)model
- the model attributes for the current view (can benull
, using the request attributes for Errors retrieval)- See Also:
-
-
Method Details
-
getRequest
Return the underlying HttpServletRequest. Only intended for cooperating classes in this package. -
getServletContext
Return the underlying ServletContext. Only intended for cooperating classes in this package. -
getWebApplicationContext
Return the current WebApplicationContext. -
getModel
Return the model Map that this RequestContext encapsulates, if any.- Returns:
- the populated model Map, or
null
if none available
-
getMessageSource
Return the MessageSource to use (typically the current WebApplicationContext).Note: As of 6.2, this method is non-final and therefore overridable.
-
getLocale
Return the current Locale (falling back to the request locale; nevernull
).Typically coming from a DispatcherServlet's
LocaleResolver
. Also includes a fallback check for JSTL's Locale attribute.Note: As of 6.2, this method is non-final and therefore overridable.
-
getTimeZone
Return the current TimeZone (ornull
if none derivable from the request).Typically coming from a DispatcherServlet's
LocaleContextResolver
. Also includes a fallback check for JSTL's TimeZone attribute. -
getFallbackLocale
Determine the fallback locale for this context.The default implementation checks for a JSTL locale attribute in request, session or application scope; if not found, returns the
HttpServletRequest.getLocale()
.- Returns:
- the fallback locale (never
null
) - See Also:
-
getFallbackTimeZone
Determine the fallback time zone for this context.The default implementation checks for a JSTL time zone attribute in request, session or application scope; returns
null
if not found.- Returns:
- the fallback time zone (or
null
if none derivable from the request)
-
changeLocale
Change the current locale to the specified one, storing the new locale through the configuredLocaleResolver
.- Parameters:
locale
- the new locale- See Also:
-
changeLocale
Change the current locale to the specified locale and time zone context, storing the new locale context through the configuredLocaleResolver
.- Parameters:
locale
- the new localetimeZone
- the new time zone- See Also:
-
setDefaultHtmlEscape
public void setDefaultHtmlEscape(boolean defaultHtmlEscape) (De)activate default HTML escaping for messages and errors, for the scope of this RequestContext.The default is the application-wide setting (the "defaultHtmlEscape" context-param in web.xml).
-
isDefaultHtmlEscape
public boolean isDefaultHtmlEscape()Is default HTML escaping active? Falls back tofalse
in case of no explicit default given. -
getDefaultHtmlEscape
Return the default HTML escape setting, differentiating between no default specified and an explicit value.- Returns:
- whether default HTML escaping is enabled (null = no explicit default)
-
isResponseEncodedHtmlEscape
public boolean isResponseEncodedHtmlEscape()Is HTML escaping using the response encoding by default? If enabled, only XML markup significant characters will be escaped with UTF-* encodings.Falls back to
true
in case of no explicit default given, as of Spring 4.2.- Since:
- 4.1.2
-
getResponseEncodedHtmlEscape
Return the default setting about use of response encoding for HTML escape setting, differentiating between no default specified and an explicit value.- Returns:
- whether default use of response encoding HTML escaping is enabled (null = no explicit default)
- Since:
- 4.1.2
-
setUrlPathHelper
Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.Set the UrlPathHelper to use for context path and request URI decoding. Can be used to pass a shared UrlPathHelper instance in.A default UrlPathHelper is always available.
-
getUrlPathHelper
Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.Return the UrlPathHelper used for context path and request URI decoding. Can be used to configure the current UrlPathHelper.A default UrlPathHelper is always available.
-
getRequestDataValueProcessor
Return the RequestDataValueProcessor instance to use obtained from the WebApplicationContext under the name"requestDataValueProcessor"
. Ornull
if no matching bean was found. -
getContextPath
Return the context path of the original request, that is, the path that indicates the current web application. This is useful for building links to other resources within the application.Delegates to the UrlPathHelper for decoding.
-
getContextUrl
Return a context-aware URl for the given relative URL.- Parameters:
relativeUrl
- the relative URL part- Returns:
- a URL that points back to the server with an absolute path (also URL-encoded accordingly)
-
getContextUrl
Return a context-aware URl for the given relative URL with placeholders (named keys with braces{}
). For example, send in a relative URLfoo/{bar}?spam={spam}
and a parameter map{bar=baz,spam=nuts}
and the result will be[contextpath]/foo/baz?spam=nuts
.- Parameters:
relativeUrl
- the relative URL partparams
- a map of parameters to insert as placeholders in the url- Returns:
- a URL that points back to the server with an absolute path (also URL-encoded accordingly)
-
getPathToServlet
Return the path to URL mappings within the current servlet including the context path and the servlet path of the original request. This is useful for building links to other resources within the application where a servlet mapping of the style"/main/*"
is used.Delegates to the UrlPathHelper to determine the context and servlet path.
-
getRequestUri
Return the request URI of the original request, that is, the invoked URL without parameters. This is particularly useful as HTML form action target, possibly in combination with the original query string.Delegates to the UrlPathHelper for decoding.
-
getQueryString
Return the query string of the current request, that is, the part after the request path. This is particularly useful for building an HTML form action target in combination with the original request URI.Delegates to the UrlPathHelper for decoding.
-
getMessage
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- Parameters:
code
- the code of the messagedefaultMessage
- the String to return if the lookup fails- Returns:
- the message
-
getMessage
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- Parameters:
code
- the code of the messageargs
- arguments for the message, ornull
if nonedefaultMessage
- the String to return if the lookup fails- Returns:
- the message
-
getMessage
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- Parameters:
code
- the code of the messageargs
- arguments for the message as a List, ornull
if nonedefaultMessage
- the String to return if the lookup fails- Returns:
- the message
-
getMessage
public String getMessage(String code, Object @Nullable [] args, String defaultMessage, boolean htmlEscape) Retrieve the message for the given code.- Parameters:
code
- the code of the messageargs
- arguments for the message, ornull
if nonedefaultMessage
- the String to return if the lookup failshtmlEscape
- if the message should be HTML-escaped- Returns:
- the message
-
getMessage
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- Parameters:
code
- the code of the message- Returns:
- the message
- Throws:
NoSuchMessageException
- if not found
-
getMessage
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- Parameters:
code
- the code of the messageargs
- arguments for the message, ornull
if none- Returns:
- the message
- Throws:
NoSuchMessageException
- if not found
-
getMessage
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- Parameters:
code
- the code of the messageargs
- arguments for the message as a List, ornull
if none- Returns:
- the message
- Throws:
NoSuchMessageException
- if not found
-
getMessage
public String getMessage(String code, Object @Nullable [] args, boolean htmlEscape) throws NoSuchMessageException Retrieve the message for the given code.- Parameters:
code
- the code of the messageargs
- arguments for the message, ornull
if nonehtmlEscape
- if the message should be HTML-escaped- Returns:
- the message
- Throws:
NoSuchMessageException
- if not found
-
getMessage
Retrieve the given MessageSourceResolvable (for example, an ObjectError instance), using the "defaultHtmlEscape" setting.- Parameters:
resolvable
- the MessageSourceResolvable- Returns:
- the message
- Throws:
NoSuchMessageException
- if not found
-
getMessage
public String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException Retrieve the given MessageSourceResolvable (for example, an ObjectError instance).- Parameters:
resolvable
- the MessageSourceResolvablehtmlEscape
- if the message should be HTML-escaped- Returns:
- the message
- Throws:
NoSuchMessageException
- if not found
-
getErrors
Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.- Parameters:
name
- the name of the bind object- Returns:
- the Errors instance, or
null
if not found
-
getErrors
Retrieve the Errors instance for the given bind object.- Parameters:
name
- the name of the bind objecthtmlEscape
- create an Errors instance with automatic HTML escaping?- Returns:
- the Errors instance, or
null
if not found
-
getModelObject
Retrieve the model object for the given model name, either from the model or from the request attributes.- Parameters:
modelName
- the name of the model object- Returns:
- the model object
-
getBindStatus
Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.- Parameters:
path
- the bean and property path for which values and errors will be resolved (for example, "person.age")- Returns:
- the new BindStatus instance
- Throws:
IllegalStateException
- if no corresponding Errors object found
-
getBindStatus
Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.- Parameters:
path
- the bean and property path for which values and errors will be resolved (for example, "person.age")htmlEscape
- create a BindStatus with automatic HTML escaping?- Returns:
- the new BindStatus instance
- Throws:
IllegalStateException
- if no corresponding Errors object found
-
PathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.