public class RequestContext
extends java.lang.Object
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 of DispatcherServlet requests, accessing the root WebApplicationContext and using an appropriate fallback for the locale (the HttpServletRequest's primary locale).
DispatcherServlet
,
AbstractView.setRequestContextAttribute(java.lang.String)
,
UrlBasedViewResolver.setRequestContextAttribute(java.lang.String)
,
getFallbackLocale()
Modifier and Type | Class and Description |
---|---|
private static class |
RequestContext.JstlLocaleResolver
Inner class that isolates the JSTL dependency.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_THEME_NAME
Default theme name used if the RequestContext cannot find a ThemeResolver.
|
private java.lang.Boolean |
defaultHtmlEscape |
private java.util.Map<java.lang.String,Errors> |
errorsMap |
protected static boolean |
jstlPresent |
private java.util.Locale |
locale |
private java.util.Map<java.lang.String,java.lang.Object> |
model |
private HttpServletRequest |
request |
private RequestDataValueProcessor |
requestDataValueProcessor |
private HttpServletResponse |
response |
private java.lang.Boolean |
responseEncodedHtmlEscape |
private Theme |
theme |
private java.util.TimeZone |
timeZone |
private UrlPathHelper |
urlPathHelper |
static java.lang.String |
WEB_APPLICATION_CONTEXT_ATTRIBUTE
Request attribute to hold the current web application context for RequestContext usage.
|
private WebApplicationContext |
webApplicationContext |
Modifier | Constructor and Description |
---|---|
protected |
RequestContext()
Default constructor for subclasses.
|
|
RequestContext(HttpServletRequest request)
Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
|
|
RequestContext(HttpServletRequest request,
HttpServletResponse response)
Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
|
|
RequestContext(HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext,
java.util.Map<java.lang.String,java.lang.Object> model)
Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
|
|
RequestContext(HttpServletRequest request,
java.util.Map<java.lang.String,java.lang.Object> model)
Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
|
|
RequestContext(HttpServletRequest request,
ServletContext servletContext)
Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
|
Modifier and Type | Method and Description |
---|---|
void |
changeLocale(java.util.Locale locale)
Change the current locale to the specified one,
storing the new locale through the configured
LocaleResolver . |
void |
changeLocale(java.util.Locale locale,
java.util.TimeZone timeZone)
Change the current locale to the specified locale and time zone context,
storing the new locale context through the configured
LocaleResolver . |
void |
changeTheme(java.lang.String themeName)
Change the current theme to the specified theme by name,
storing the new theme name through the configured
ThemeResolver . |
void |
changeTheme(Theme theme)
Change the current theme to the specified one,
storing the new theme name through the configured
ThemeResolver . |
BindStatus |
getBindStatus(java.lang.String path)
Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.
|
BindStatus |
getBindStatus(java.lang.String path,
boolean htmlEscape)
Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.
|
java.lang.String |
getContextPath()
Return the context path of the original request, that is, the path that
indicates the current web application.
|
java.lang.String |
getContextUrl(java.lang.String relativeUrl)
Return a context-aware URl for the given relative URL.
|
java.lang.String |
getContextUrl(java.lang.String relativeUrl,
java.util.Map<java.lang.String,?> params)
Return a context-aware URl for the given relative URL with placeholders (named keys with braces
{} ). |
java.lang.Boolean |
getDefaultHtmlEscape()
Return the default HTML escape setting, differentiating between no default specified and an explicit value.
|
Errors |
getErrors(java.lang.String name)
Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.
|
Errors |
getErrors(java.lang.String name,
boolean htmlEscape)
Retrieve the Errors instance for the given bind object.
|
protected java.util.Locale |
getFallbackLocale()
Determine the fallback locale for this context.
|
protected Theme |
getFallbackTheme()
Determine the fallback theme for this context.
|
protected java.util.TimeZone |
getFallbackTimeZone()
Determine the fallback time zone for this context.
|
java.util.Locale |
getLocale()
Return the current Locale (falling back to the request locale; never
null ). |
java.lang.String |
getMessage(MessageSourceResolvable resolvable)
Retrieve the given MessageSourceResolvable (e.g.
|
java.lang.String |
getMessage(MessageSourceResolvable resolvable,
boolean htmlEscape)
Retrieve the given MessageSourceResolvable (e.g.
|
java.lang.String |
getMessage(java.lang.String code)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
|
java.lang.String |
getMessage(java.lang.String code,
java.util.List<?> args)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
|
java.lang.String |
getMessage(java.lang.String code,
java.util.List<?> args,
java.lang.String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
|
java.lang.String |
getMessage(java.lang.String code,
java.lang.Object[] args)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
|
java.lang.String |
getMessage(java.lang.String code,
java.lang.Object[] args,
boolean htmlEscape)
Retrieve the message for the given code.
|
java.lang.String |
getMessage(java.lang.String code,
java.lang.Object[] args,
java.lang.String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
|
java.lang.String |
getMessage(java.lang.String code,
java.lang.Object[] args,
java.lang.String defaultMessage,
boolean htmlEscape)
Retrieve the message for the given code.
|
java.lang.String |
getMessage(java.lang.String code,
java.lang.String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
|
MessageSource |
getMessageSource()
Return the current WebApplicationContext as MessageSource.
|
java.util.Map<java.lang.String,java.lang.Object> |
getModel()
Return the model Map that this RequestContext encapsulates, if any.
|
protected java.lang.Object |
getModelObject(java.lang.String modelName)
Retrieve the model object for the given model name, either from the model or from the request attributes.
|
java.lang.String |
getPathToServlet()
Return the path to URL mappings within the current servlet including the
context path and the servlet path of the original request.
|
java.lang.String |
getQueryString()
Return the query string of the current request, that is, the part after
the request path.
|
protected HttpServletRequest |
getRequest()
Return the underlying HttpServletRequest.
|
RequestDataValueProcessor |
getRequestDataValueProcessor()
Return the RequestDataValueProcessor instance to use obtained from the
WebApplicationContext under the name
"requestDataValueProcessor" . |
java.lang.String |
getRequestUri()
Return the request URI of the original request, that is, the invoked URL
without parameters.
|
java.lang.Boolean |
getResponseEncodedHtmlEscape()
Return the default setting about use of response encoding for HTML escape setting,
differentiating between no default specified and an explicit value.
|
protected ServletContext |
getServletContext()
Return the underlying ServletContext.
|
Theme |
getTheme()
Return the current theme (never
null ). |
java.lang.String |
getThemeMessage(MessageSourceResolvable resolvable)
Retrieve the given MessageSourceResolvable in the current theme.
|
java.lang.String |
getThemeMessage(java.lang.String code)
Retrieve the theme message for the given code.
|
java.lang.String |
getThemeMessage(java.lang.String code,
java.util.List<?> args)
Retrieve the theme message for the given code.
|
java.lang.String |
getThemeMessage(java.lang.String code,
java.util.List<?> args,
java.lang.String defaultMessage)
Retrieve the theme message for the given code.
|
java.lang.String |
getThemeMessage(java.lang.String code,
java.lang.Object[] args)
Retrieve the theme message for the given code.
|
java.lang.String |
getThemeMessage(java.lang.String code,
java.lang.Object[] args,
java.lang.String defaultMessage)
Retrieve the theme message for the given code.
|
java.lang.String |
getThemeMessage(java.lang.String code,
java.lang.String defaultMessage)
Retrieve the theme message for the given code.
|
java.util.TimeZone |
getTimeZone()
Return the current TimeZone (or
null if none derivable from the request). |
UrlPathHelper |
getUrlPathHelper()
Return the UrlPathHelper used for context path and request URI decoding.
|
WebApplicationContext |
getWebApplicationContext()
Return the current WebApplicationContext.
|
protected void |
initContext(HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext,
java.util.Map<java.lang.String,java.lang.Object> model)
Initialize this context with the given request, using the given model attributes for Errors retrieval.
|
boolean |
isDefaultHtmlEscape()
Is default HTML escaping active? Falls back to
false in case of no explicit default given. |
boolean |
isResponseEncodedHtmlEscape()
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)
Set the UrlPathHelper to use for context path and request URI decoding.
|
public static final java.lang.String DEFAULT_THEME_NAME
Same as AbstractThemeResolver's default, but not linked in here to avoid package interdependencies.
public static final java.lang.String WEB_APPLICATION_CONTEXT_ATTRIBUTE
protected static final boolean jstlPresent
private HttpServletRequest request
private HttpServletResponse response
private java.util.Map<java.lang.String,java.lang.Object> model
private WebApplicationContext webApplicationContext
private java.util.Locale locale
private java.util.TimeZone timeZone
private Theme theme
private java.lang.Boolean defaultHtmlEscape
private java.lang.Boolean responseEncodedHtmlEscape
private UrlPathHelper urlPathHelper
private RequestDataValueProcessor requestDataValueProcessor
private java.util.Map<java.lang.String,Errors> errorsMap
public RequestContext(HttpServletRequest request)
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.
Will only work within a DispatcherServlet request. Pass in a ServletContext to be able to fallback to the root WebApplicationContext.
request
- current HTTP requestDispatcherServlet
,
#RequestContext(javax.servlet.http.HttpServletRequest, javax.servlet.ServletContext)
public RequestContext(HttpServletRequest request, HttpServletResponse response)
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.
Will only work within a DispatcherServlet request. Pass in a ServletContext to be able to fallback to the root WebApplicationContext.
request
- current HTTP requestresponse
- current HTTP responseDispatcherServlet
,
#RequestContext(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletContext, Map)
public RequestContext(HttpServletRequest request, ServletContext servletContext)
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).
request
- current HTTP requestservletContext
- the servlet context of the web application (can be null
;
necessary for fallback to root WebApplicationContext)WebApplicationContext
,
DispatcherServlet
public RequestContext(HttpServletRequest request, java.util.Map<java.lang.String,java.lang.Object> model)
This works with all View implementations. It will typically be used by View implementations.
Will only work within a DispatcherServlet request. Pass in a ServletContext to be able to fallback to the root WebApplicationContext.
request
- current HTTP requestmodel
- the model attributes for the current view (can be null
,
using the request attributes for Errors retrieval)DispatcherServlet
,
#RequestContext(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletContext, Map)
public RequestContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, java.util.Map<java.lang.String,java.lang.Object> model)
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).
request
- current HTTP requestresponse
- current HTTP responseservletContext
- the servlet context of the web application (can be null
; necessary for
fallback to root WebApplicationContext)model
- the model attributes for the current view (can be null
, using the request attributes
for Errors retrieval)WebApplicationContext
,
DispatcherServlet
protected RequestContext()
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, java.util.Map<java.lang.String,java.lang.Object> model)
Delegates to getFallbackLocale
and getFallbackTheme
for determining the fallback
locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
request
- current HTTP requestservletContext
- the servlet context of the web application (can be null
; necessary for
fallback to root WebApplicationContext)model
- the model attributes for the current view (can be null
, using the request attributes
for Errors retrieval)getFallbackLocale()
,
getFallbackTheme()
,
DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE
,
DispatcherServlet.THEME_RESOLVER_ATTRIBUTE
protected java.util.Locale getFallbackLocale()
The default implementation checks for a JSTL locale attribute in request, session
or application scope; if not found, returns the HttpServletRequest.getLocale()
.
null
)javax.servlet.http.HttpServletRequest#getLocale()
protected java.util.TimeZone getFallbackTimeZone()
The default implementation checks for a JSTL time zone attribute in request,
session or application scope; returns null
if not found.
null
if none derivable from the request)protected Theme getFallbackTheme()
The default implementation returns the default theme (with name "theme").
null
)protected final HttpServletRequest getRequest()
protected final ServletContext getServletContext()
public final WebApplicationContext getWebApplicationContext()
public final MessageSource getMessageSource()
public final java.util.Map<java.lang.String,java.lang.Object> getModel()
null
if none availablepublic final java.util.Locale getLocale()
null
).
Typically coming from a DispatcherServlet's LocaleResolver
.
Also includes a fallback check for JSTL's Locale attribute.
public java.util.TimeZone getTimeZone()
null
if none derivable from the request).
Typically coming from a DispatcherServlet's LocaleContextResolver
.
Also includes a fallback check for JSTL's TimeZone attribute.
public void changeLocale(java.util.Locale locale)
LocaleResolver
.locale
- the new localeLocaleResolver.setLocale(HttpServletRequest, HttpServletResponse, java.util.Locale)
,
changeLocale(java.util.Locale, java.util.TimeZone)
public void changeLocale(java.util.Locale locale, java.util.TimeZone timeZone)
LocaleResolver
.locale
- the new localetimeZone
- the new time zoneLocaleContextResolver.setLocaleContext(HttpServletRequest, HttpServletResponse, org.springframework.context.i18n.LocaleContext)
,
SimpleTimeZoneAwareLocaleContext
public Theme getTheme()
null
).
Resolved lazily for more efficiency when theme support is not being used.
public void changeTheme(Theme theme)
ThemeResolver
.theme
- the new themeThemeResolver.setThemeName(HttpServletRequest, HttpServletResponse, java.lang.String)
public void changeTheme(java.lang.String themeName)
ThemeResolver
.themeName
- the name of the new themeThemeResolver.setThemeName(HttpServletRequest, HttpServletResponse, java.lang.String)
public void setDefaultHtmlEscape(boolean defaultHtmlEscape)
The default is the application-wide setting (the "defaultHtmlEscape" context-param in web.xml).
public boolean isDefaultHtmlEscape()
false
in case of no explicit default given.public java.lang.Boolean getDefaultHtmlEscape()
public boolean isResponseEncodedHtmlEscape()
Falls back to true
in case of no explicit default given, as of Spring 4.2.
public java.lang.Boolean getResponseEncodedHtmlEscape()
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
A default UrlPathHelper is always available.
public UrlPathHelper getUrlPathHelper()
A default UrlPathHelper is always available.
public RequestDataValueProcessor getRequestDataValueProcessor()
"requestDataValueProcessor"
.
Or null
if no matching bean was found.public java.lang.String getContextPath()
Delegates to the UrlPathHelper for decoding.
javax.servlet.http.HttpServletRequest#getContextPath
,
getUrlPathHelper()
public java.lang.String getContextUrl(java.lang.String relativeUrl)
relativeUrl
- the relative URL partpublic java.lang.String getContextUrl(java.lang.String relativeUrl, java.util.Map<java.lang.String,?> params)
{}
).
For example, send in a relative URL foo/{bar}?spam={spam}
and a parameter map
{bar=baz,spam=nuts}
and the result will be [contextpath]/foo/baz?spam=nuts
.relativeUrl
- the relative URL partparams
- a map of parameters to insert as placeholders in the urlpublic java.lang.String getPathToServlet()
"/main/*"
is used.
Delegates to the UrlPathHelper to determine the context and servlet path.
public java.lang.String getRequestUri()
Delegates to the UrlPathHelper for decoding.
public java.lang.String getQueryString()
Delegates to the UrlPathHelper for decoding.
public java.lang.String getMessage(java.lang.String code, java.lang.String defaultMessage)
code
- code of the messagedefaultMessage
- String to return if the lookup failspublic java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage)
code
- code of the messageargs
- arguments for the message, or null
if nonedefaultMessage
- String to return if the lookup failspublic java.lang.String getMessage(java.lang.String code, java.util.List<?> args, java.lang.String defaultMessage)
code
- code of the messageargs
- arguments for the message as a List, or null
if nonedefaultMessage
- String to return if the lookup failspublic java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage, boolean htmlEscape)
code
- code of the messageargs
- arguments for the message, or null
if nonedefaultMessage
- String to return if the lookup failshtmlEscape
- HTML escape the message?public java.lang.String getMessage(java.lang.String code) throws NoSuchMessageException
code
- code of the messageNoSuchMessageException
- if not foundpublic java.lang.String getMessage(java.lang.String code, java.lang.Object[] args) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message, or null
if noneNoSuchMessageException
- if not foundpublic java.lang.String getMessage(java.lang.String code, java.util.List<?> args) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message as a List, or null
if noneNoSuchMessageException
- if not foundpublic java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, boolean htmlEscape) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message, or null
if nonehtmlEscape
- HTML escape the message?NoSuchMessageException
- if not foundpublic java.lang.String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException
resolvable
- the MessageSourceResolvableNoSuchMessageException
- if not foundpublic java.lang.String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException
resolvable
- the MessageSourceResolvablehtmlEscape
- HTML escape the message?NoSuchMessageException
- if not foundpublic java.lang.String getThemeMessage(java.lang.String code, java.lang.String defaultMessage)
Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.
code
- code of the messagedefaultMessage
- String to return if the lookup failspublic java.lang.String getThemeMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage)
Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.
code
- code of the messageargs
- arguments for the message, or null
if nonedefaultMessage
- String to return if the lookup failspublic java.lang.String getThemeMessage(java.lang.String code, java.util.List<?> args, java.lang.String defaultMessage)
Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.
code
- code of the messageargs
- arguments for the message as a List, or null
if nonedefaultMessage
- String to return if the lookup failspublic java.lang.String getThemeMessage(java.lang.String code) throws NoSuchMessageException
Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.
code
- code of the messageNoSuchMessageException
- if not foundpublic java.lang.String getThemeMessage(java.lang.String code, java.lang.Object[] args) throws NoSuchMessageException
Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.
code
- code of the messageargs
- arguments for the message, or null
if noneNoSuchMessageException
- if not foundpublic java.lang.String getThemeMessage(java.lang.String code, java.util.List<?> args) throws NoSuchMessageException
Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.
code
- code of the messageargs
- arguments for the message as a List, or null
if noneNoSuchMessageException
- if not foundpublic java.lang.String getThemeMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException
Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.
resolvable
- the MessageSourceResolvableNoSuchMessageException
- if not foundpublic Errors getErrors(java.lang.String name)
name
- name of the bind objectnull
if not foundpublic Errors getErrors(java.lang.String name, boolean htmlEscape)
name
- name of the bind objecthtmlEscape
- create an Errors instance with automatic HTML escaping?null
if not foundprotected java.lang.Object getModelObject(java.lang.String modelName)
modelName
- the name of the model objectpublic BindStatus getBindStatus(java.lang.String path) throws java.lang.IllegalStateException
path
- the bean and property path for which values and errors will be resolved (e.g. "person.age")java.lang.IllegalStateException
- if no corresponding Errors object foundpublic BindStatus getBindStatus(java.lang.String path, boolean htmlEscape) throws java.lang.IllegalStateException
path
- the bean and property path for which values and errors will be resolved (e.g. "person.age")htmlEscape
- create a BindStatus with automatic HTML escaping?java.lang.IllegalStateException
- if no corresponding Errors object found