public class RequestContext
extends java.lang.Object
MessageSource
to
use, current locale, binding errors, etc. Provides easy access to localized
messages and Errors instances.
Suitable for exposition to views, and usage within FreeMarker templates, and tag libraries.
Can be instantiated manually, or automatically exposed to views as model attribute via AbstractView's "requestContextAttribute" property.
Constructor and Description |
---|
RequestContext(ServerWebExchange exchange,
java.util.Map<java.lang.String,java.lang.Object> model,
MessageSource messageSource) |
RequestContext(ServerWebExchange exchange,
java.util.Map<java.lang.String,java.lang.Object> model,
MessageSource messageSource,
RequestDataValueProcessor dataValueProcessor) |
Modifier and Type | Method and Description |
---|---|
void |
changeLocale(java.util.Locale locale)
Change the current locale to the specified one.
|
void |
changeLocale(java.util.Locale locale,
java.util.TimeZone timeZone)
Change the current locale to the specified locale and time zone context.
|
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 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 ServerWebExchange |
getExchange() |
java.util.Locale |
getLocale()
Return the current Locale.
|
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 MessageSource in use with this request.
|
java.util.Map<java.lang.String,java.lang.Object> |
getModel()
Return the model Map that this RequestContext encapsulates, if any.
|
protected <T> T |
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 |
getQueryString()
Return the query string of the current request.
|
RequestDataValueProcessor |
getRequestDataValueProcessor()
Return the
RequestDataValueProcessor instance to apply to in form
tag libraries and to redirect URLs. |
java.lang.String |
getRequestPath()
Return the request path of the request.
|
java.util.TimeZone |
getTimeZone()
Return the current TimeZone.
|
boolean |
isDefaultHtmlEscape()
Is default HTML escaping active? Falls back to
false in case of
no explicit default given. |
void |
setDefaultHtmlEscape(boolean defaultHtmlEscape)
(De)activate default HTML escaping for messages and errors, for the scope
of this RequestContext.
|
public RequestContext(ServerWebExchange exchange, java.util.Map<java.lang.String,java.lang.Object> model, MessageSource messageSource)
public RequestContext(ServerWebExchange exchange, java.util.Map<java.lang.String,java.lang.Object> model, MessageSource messageSource, @Nullable RequestDataValueProcessor dataValueProcessor)
protected final ServerWebExchange getExchange()
public MessageSource getMessageSource()
@Nullable public java.util.Map<java.lang.String,java.lang.Object> getModel()
null
if none availablepublic final java.util.Locale getLocale()
public java.util.TimeZone getTimeZone()
public void changeLocale(java.util.Locale locale)
public void changeLocale(java.util.Locale locale, java.util.TimeZone timeZone)
public void setDefaultHtmlEscape(boolean defaultHtmlEscape)
TODO: currently no application-wide setting ...
public boolean isDefaultHtmlEscape()
false
in case of
no explicit default given.@Nullable public java.lang.Boolean getDefaultHtmlEscape()
@Nullable public RequestDataValueProcessor getRequestDataValueProcessor()
RequestDataValueProcessor
instance to apply to in form
tag libraries and to redirect URLs.public java.lang.String getContextPath()
Delegates to ServerHttpRequest.getPath()
.
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 getRequestPath()
public java.lang.String getQueryString()
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, @Nullable 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, @Nullable 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, @Nullable 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, @Nullable 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, @Nullable 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, @Nullable 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 found@Nullable public Errors getErrors(java.lang.String name)
name
- name of the bind objectnull
if not found@Nullable public 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 found@Nullable protected <T> T 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