|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.web.servlet.support.RequestContext
public class RequestContext
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, Velocity templates, etc. Necessary for views that do not have access to the servlet request, like Velocity 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 JSTL locale if available, or the HttpServletRequest locale else).
DispatcherServlet
,
AbstractView.setRequestContextAttribute(java.lang.String)
,
UrlBasedViewResolver.setRequestContextAttribute(java.lang.String)
,
getFallbackLocale()
Field Summary | |
---|---|
protected static String |
APPLICATION_SCOPE_SUFFIX
JSTL suffix for application-scoped attributes |
static String |
DEFAULT_THEME_NAME
Default theme name used if the RequestContext cannot find a ThemeResolver. |
static String |
JSTL_LOCALE_ATTRIBUTE
JSTL locale attribute, as used by JSTL implementations to expose their current locale. |
protected static String |
REQUEST_SCOPE_SUFFIX
JSTL suffix for request-scoped attributes |
protected static String |
SESSION_SCOPE_SUFFIX
JSTL suffix for session-scoped attributes |
Constructor Summary | |
---|---|
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,
Map 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. |
|
RequestContext(HttpServletRequest request,
ServletContext servletContext,
Map model)
Create a new RequestContext for the given request, using the given model attributes for Errors retrieval. |
Method Summary | |
---|---|
BindStatus |
getBindStatus(String path)
Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting. |
BindStatus |
getBindStatus(String path,
boolean htmlEscape)
Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting. |
String |
getContextPath()
Return the context path of the original request, that is, the path that indicates the current web application. |
Errors |
getErrors(String name)
Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting. |
Errors |
getErrors(String name,
boolean htmlEscape)
Retrieve the Errors instance for the given bind object. |
protected Locale |
getFallbackLocale()
Determine the fallback locale for this context. |
protected Theme |
getFallbackTheme()
Determine the fallback theme for this context. |
Locale |
getLocale()
Return the current locale. |
String |
getMessage(MessageSourceResolvable resolvable)
Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the "defaultHtmlEscape" setting. |
String |
getMessage(MessageSourceResolvable resolvable,
boolean htmlEscape)
Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance). |
String |
getMessage(String code)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting. |
String |
getMessage(String code,
List args)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting. |
String |
getMessage(String code,
List args,
String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting. |
String |
getMessage(String code,
Object[] args)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting. |
String |
getMessage(String code,
Object[] args,
boolean htmlEscape)
Retrieve the message for the given code. |
String |
getMessage(String code,
Object[] args,
String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting. |
String |
getMessage(String code,
Object[] args,
String defaultMessage,
boolean htmlEscape)
Retrieve the message for the given code. |
String |
getMessage(String code,
String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting. |
protected Object |
getModelObject(String modelName)
Retrieve the model object for the given model name, either from the model or from the request attributes. |
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. |
String |
getRequestUri()
Return the request URI of the original request, that is, the invoked URL without parameters. |
protected ServletContext |
getServletContext()
Return the underlying ServletContext. |
Theme |
getTheme()
Return the current theme. |
String |
getThemeMessage(MessageSourceResolvable resolvable)
Retrieve the given MessageSourceResolvable in the current theme. |
String |
getThemeMessage(String code)
Retrieve the theme message for the given code. |
String |
getThemeMessage(String code,
List args)
Retrieve the theme message for the given code. |
String |
getThemeMessage(String code,
List args,
String defaultMessage)
Retrieve the theme message for the given code. |
String |
getThemeMessage(String code,
Object[] args)
Retrieve the theme message for the given code. |
String |
getThemeMessage(String code,
Object[] args,
String defaultMessage)
Retrieve the theme message for the given code. |
String |
getThemeMessage(String code,
String defaultMessage)
Retrieve the theme message for the given code. |
UrlPathHelper |
getUrlPathHelper()
Return the UrlPathHelper used for context path and request URI decoding. |
WebApplicationContext |
getWebApplicationContext()
Return the current WebApplicationContext. |
protected void |
initContext(HttpServletRequest request,
ServletContext servletContext,
Map model)
Initialize this context with the given request, using the given model attributes for Errors retrieval. |
boolean |
isDefaultHtmlEscape()
Is default HTML escaping active? |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_THEME_NAME
Same as AbstractThemeResolver's default, but not linked in here to avoid package interdependencies.
public static final String JSTL_LOCALE_ATTRIBUTE
request.getLocale
Same as the FMT_LOCALE constant in JSTL's Config class, but not linked
in here to avoid a hard-coded dependency on JSTL. RequestContext does not
depend on JSTL except for this fallback check of JSTL's locale attribute.
Config.FMT_LOCALE
,
ServletRequest.getLocale()
,
Constant Field Valuesprotected static final String REQUEST_SCOPE_SUFFIX
protected static final String SESSION_SCOPE_SUFFIX
protected static final String APPLICATION_SCOPE_SUFFIX
Constructor Detail |
---|
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, 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, Map 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.ServletContext, Map)
public RequestContext(HttpServletRequest request, ServletContext servletContext, Map 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 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)WebApplicationContext
,
DispatcherServlet
protected RequestContext()
Method Detail |
---|
protected void initContext(HttpServletRequest request, ServletContext servletContext, Map 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 Locale getFallbackLocale()
Default implementation checks for a JSTL locale attribute
in request, session or application scope; if not found,
returns the HttpServletRequest.getLocale()
.
null
)ServletRequest.getLocale()
protected Theme getFallbackTheme()
Default implementation returns the default theme (with name "theme").
null
protected final HttpServletRequest getRequest()
public final WebApplicationContext getWebApplicationContext()
protected final ServletContext getServletContext()
public final Locale getLocale()
public final Theme getTheme()
public void setDefaultHtmlEscape(boolean defaultHtmlEscape)
public boolean isDefaultHtmlEscape()
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
A default UrlPathHelper is always available.
public UrlPathHelper getUrlPathHelper()
A default UrlPathHelper is always available.
public String getContextPath()
Delegates to the UrlPathHelper for decoding.
public String getRequestUri()
Note this implementation will correctly resolve to the URI of any
originating root request in the presence of a forwarded request. However, this
can only work when the Servlet 2.4 'forward' request attributes are present.
For use in a Servlet 2.3- environment, you can rely on
InternalResourceView
to add these prior to dispatching the request.
Delegates to the UrlPathHelper for decoding.
public String getQueryString()
Note this implementation will correctly resolve to the query string of any
originating root request in the presence of a forwarded request. However, this
can only work when the Servlet 2.4 'forward' request attributes are present.
For use in a Servlet 2.3- environment, you can rely on
InternalResourceView
to add these prior to dispatching the request.
Delegates to the UrlPathHelper for decoding.
public String getMessage(String code, String defaultMessage)
code
- code of the messagedefaultMessage
- String to return if the lookup fails
public String getMessage(String code, Object[] args, String defaultMessage)
code
- code of the messageargs
- arguments for the message, or null
if nonedefaultMessage
- String to return if the lookup fails
public String getMessage(String code, List args, String defaultMessage)
code
- code of the messageargs
- arguments for the message as a List, or null
if nonedefaultMessage
- String to return if the lookup fails
public String getMessage(String code, Object[] args, 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 String getMessage(String code) throws NoSuchMessageException
code
- code of the message
NoSuchMessageException
- if not foundpublic String getMessage(String code, Object[] args) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message, or null
if none
NoSuchMessageException
- if not foundpublic String getMessage(String code, List args) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message as a List, or null
if none
NoSuchMessageException
- if not foundpublic String getMessage(String code, 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 String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException
resolvable
- the MessageSourceResolvable
NoSuchMessageException
- if not foundpublic String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException
resolvable
- the MessageSourceResolvablehtmlEscape
- HTML escape the message?
NoSuchMessageException
- if not foundpublic String getThemeMessage(String code, 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 fails
public String getThemeMessage(String code, Object[] args, 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 fails
public String getThemeMessage(String code, List args, 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 fails
public String getThemeMessage(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 message
NoSuchMessageException
- if not foundpublic String getThemeMessage(String code, 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 none
NoSuchMessageException
- if not foundpublic String getThemeMessage(String code, 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 none
NoSuchMessageException
- if not foundpublic 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 MessageSourceResolvable
NoSuchMessageException
- if not foundpublic Errors getErrors(String name)
name
- name of the bind object
null
if not foundpublic Errors getErrors(String name, boolean htmlEscape)
name
- name of the bind objecthtmlEscape
- create an Errors instance with automatic HTML escaping?
null
if not foundprotected Object getModelObject(String modelName)
modelName
- the name of the model object
public BindStatus getBindStatus(String path) throws IllegalStateException
path
- the bean and property path for which values and errors
will be resolved (e.g. "person.age")
IllegalStateException
- if no corresponding Errors object foundpublic BindStatus getBindStatus(String path, boolean htmlEscape) throws 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?
IllegalStateException
- if no corresponding Errors object found
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |