public class RequestContext extends 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 a model attribute via AbstractView's "requestContextAttribute" property.
Constructor and Description |
---|
RequestContext(ServerWebExchange exchange,
Map<String,Object> model,
MessageSource messageSource) |
RequestContext(ServerWebExchange exchange,
Map<String,Object> model,
MessageSource messageSource,
RequestDataValueProcessor dataValueProcessor) |
Modifier and Type | Method and Description |
---|---|
void |
changeLocale(Locale locale)
Change the current locale to the specified one.
|
void |
changeLocale(Locale locale,
TimeZone timeZone)
Change the current locale to the specified locale and time zone context.
|
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 current web application.
|
String |
getContextUrl(String relativeUrl)
Return a context-aware URl for the given relative URL.
|
String |
getContextUrl(String relativeUrl,
Map<String,?> params)
Return a context-aware URl for the given relative URL with placeholders --
named keys with braces
{} . |
Boolean |
getDefaultHtmlEscape()
Return the default HTML escape setting, differentiating between no default
specified and an explicit value.
|
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 ServerWebExchange |
getExchange() |
Locale |
getLocale()
Return the current Locale.
|
String |
getMessage(MessageSourceResolvable resolvable)
Retrieve the given MessageSourceResolvable (e.g.
|
String |
getMessage(MessageSourceResolvable resolvable,
boolean htmlEscape)
Retrieve the given MessageSourceResolvable (e.g.
|
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.
|
MessageSource |
getMessageSource()
Return the MessageSource in use with this request.
|
Map<String,Object> |
getModel()
Return the model Map that this RequestContext encapsulates, if any.
|
protected <T> T |
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.
|
RequestDataValueProcessor |
getRequestDataValueProcessor()
Return the
RequestDataValueProcessor instance to apply to in form
tag libraries and to redirect URLs. |
String |
getRequestPath()
Return the request path of the request.
|
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, Map<String,Object> model, MessageSource messageSource)
public RequestContext(ServerWebExchange exchange, Map<String,Object> model, MessageSource messageSource, @Nullable RequestDataValueProcessor dataValueProcessor)
protected final ServerWebExchange getExchange()
public MessageSource getMessageSource()
@Nullable public Map<String,Object> getModel()
null
if none availablepublic final Locale getLocale()
public TimeZone getTimeZone()
public void changeLocale(Locale locale)
public void changeLocale(Locale locale, 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 Boolean getDefaultHtmlEscape()
@Nullable public RequestDataValueProcessor getRequestDataValueProcessor()
RequestDataValueProcessor
instance to apply to in form
tag libraries and to redirect URLs.public String getContextPath()
Delegates to ServerHttpRequest.getPath()
.
public String getContextUrl(String relativeUrl)
relativeUrl
- the relative URL partpublic String getContextUrl(String relativeUrl, Map<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 String getRequestPath()
public String getQueryString()
public String getMessage(String code, String defaultMessage)
code
- code of the messagedefaultMessage
- the String to return if the lookup failspublic String getMessage(String code, @Nullable Object[] args, String defaultMessage)
code
- code of the messageargs
- arguments for the message, or null
if nonedefaultMessage
- the String to return if the lookup failspublic String getMessage(String code, @Nullable List<?> args, String defaultMessage)
code
- code of the messageargs
- arguments for the message as a List, or null
if nonedefaultMessage
- the String to return if the lookup failspublic String getMessage(String code, @Nullable Object[] args, String defaultMessage, boolean htmlEscape)
code
- code of the messageargs
- arguments for the message, or null
if nonedefaultMessage
- the String to return if the lookup failshtmlEscape
- if the message should be HTML-escapedpublic String getMessage(String code) throws NoSuchMessageException
code
- code of the messageNoSuchMessageException
- if not foundpublic String getMessage(String code, @Nullable Object[] args) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message, or null
if noneNoSuchMessageException
- if not foundpublic String getMessage(String code, @Nullable List<?> args) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message as a List, or null
if noneNoSuchMessageException
- if not foundpublic String getMessage(String code, @Nullable Object[] args, boolean htmlEscape) throws NoSuchMessageException
code
- code of the messageargs
- arguments for the message, or null
if nonehtmlEscape
- if the message should be HTML-escapedNoSuchMessageException
- if not foundpublic String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException
resolvable
- the MessageSourceResolvableNoSuchMessageException
- if not foundpublic String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException
resolvable
- the MessageSourceResolvablehtmlEscape
- if the message should be HTML-escapedNoSuchMessageException
- if not found@Nullable public Errors getErrors(String name)
name
- name of the bind objectnull
if not found@Nullable public Errors getErrors(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(String modelName)
modelName
- the name of the model objectpublic 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