类 RequestContext
- java.lang.Object
- org.springframework.web.reactive.result.view.RequestContext
public class RequestContext extends Object
Context holder for request-specific state, like theMessageSource
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.
- 从以下版本开始:
- 5.0
- 作者:
- Rossen Stoyanchev
构造器概要
构造器 构造器 说明 RequestContext(ServerWebExchange exchange, Map<String,Object> model, MessageSource messageSource)
RequestContext(ServerWebExchange exchange, Map<String,Object> model, MessageSource messageSource, RequestDataValueProcessor dataValueProcessor)
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 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(String code)
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.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(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).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 theRequestDataValueProcessor
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?void
setDefaultHtmlEscape(boolean defaultHtmlEscape)
(De)activate default HTML escaping for messages and errors, for the scope of this RequestContext.
构造器详细资料
RequestContext
public RequestContext(ServerWebExchange exchange, Map<String,Object> model, MessageSource messageSource)
RequestContext
public RequestContext(ServerWebExchange exchange, Map<String,Object> model, MessageSource messageSource, @Nullable RequestDataValueProcessor dataValueProcessor)
方法详细资料
getExchange
protected final ServerWebExchange getExchange()
getMessageSource
public MessageSource getMessageSource()
Return the MessageSource in use with this request.
getModel
@Nullable public Map<String,Object> getModel()
Return the model Map that this RequestContext encapsulates, if any.- 返回:
- the populated model Map, or
null
if none available
getTimeZone
public TimeZone getTimeZone()
Return the current TimeZone.
changeLocale
public void changeLocale(Locale locale)
Change the current locale to the specified one.
changeLocale
public void changeLocale(Locale locale, TimeZone timeZone)
Change the current locale to the specified locale and time zone context.
setDefaultHtmlEscape
public void setDefaultHtmlEscape(boolean defaultHtmlEscape)
(De)activate default HTML escaping for messages and errors, for the scope of this RequestContext.TODO: currently no application-wide setting ...
isDefaultHtmlEscape
public boolean isDefaultHtmlEscape()
Is default HTML escaping active? Falls back tofalse
in case of no explicit default given.
getDefaultHtmlEscape
@Nullable public Boolean getDefaultHtmlEscape()
Return the default HTML escape setting, differentiating between no default specified and an explicit value.- 返回:
- whether default HTML escaping is enabled (null = no explicit default)
getRequestDataValueProcessor
@Nullable public RequestDataValueProcessor getRequestDataValueProcessor()
Return theRequestDataValueProcessor
instance to apply to in form tag libraries and to redirect URLs.
getContextPath
public String getContextPath()
Return the context path of the current web application. This is useful for building links to other resources within the application.Delegates to
ServerHttpRequest.getPath()
.
getContextUrl
public String getContextUrl(String relativeUrl)
Return a context-aware URl for the given relative URL.- 参数:
relativeUrl
- the relative URL part- 返回:
- a URL that points back to the current web application with an absolute path also URL-encoded accordingly
getContextUrl
public String getContextUrl(String relativeUrl, Map<String,?> params)
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
.- 参数:
relativeUrl
- the relative URL partparams
- a map of parameters to insert as placeholders in the url- 返回:
- a URL that points back to the current web application with an absolute path also URL-encoded accordingly
getRequestPath
public String getRequestPath()
Return the request path of the request. This is useful as HTML form action target, also in combination with the original query string.
getQueryString
public String getQueryString()
Return the query string of the current request. This is useful for building an HTML form action target in combination with the original request path.
getMessage
public String getMessage(String code, String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- 参数:
code
- the code of the messagedefaultMessage
- the String to return if the lookup fails- 返回:
- the message
getMessage
public String getMessage(String code, @Nullable Object[] args, String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- 参数:
code
- the code of the messageargs
- arguments for the message, ornull
if nonedefaultMessage
- the String to return if the lookup fails- 返回:
- the message
getMessage
public String getMessage(String code, @Nullable List<?> args, String defaultMessage)
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- 参数:
code
- the code of the messageargs
- arguments for the message as a List, ornull
if nonedefaultMessage
- the String to return if the lookup fails- 返回:
- the message
getMessage
public String getMessage(String code, @Nullable Object[] args, String defaultMessage, boolean htmlEscape)
Retrieve the message for the given code.- 参数:
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- 返回:
- the message
getMessage
public String getMessage(String code) throws NoSuchMessageException
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- 参数:
code
- the code of the message- 返回:
- the message
- 抛出:
NoSuchMessageException
- if not found
getMessage
public String getMessage(String code, @Nullable Object[] args) throws NoSuchMessageException
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- 参数:
code
- the code of the messageargs
- arguments for the message, ornull
if none- 返回:
- the message
- 抛出:
NoSuchMessageException
- if not found
getMessage
public String getMessage(String code, @Nullable List<?> args) throws NoSuchMessageException
Retrieve the message for the given code, using the "defaultHtmlEscape" setting.- 参数:
code
- the code of the messageargs
- arguments for the message as a List, ornull
if none- 返回:
- the message
- 抛出:
NoSuchMessageException
- if not found
getMessage
public String getMessage(String code, @Nullable Object[] args, boolean htmlEscape) throws NoSuchMessageException
Retrieve the message for the given code.- 参数:
code
- the code of the messageargs
- arguments for the message, ornull
if nonehtmlEscape
- if the message should be HTML-escaped- 返回:
- the message
- 抛出:
NoSuchMessageException
- if not found
getMessage
public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException
Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the "defaultHtmlEscape" setting.- 参数:
resolvable
- the MessageSourceResolvable- 返回:
- the message
- 抛出:
NoSuchMessageException
- if not found
getMessage
public String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException
Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).- 参数:
resolvable
- the MessageSourceResolvablehtmlEscape
- if the message should be HTML-escaped- 返回:
- the message
- 抛出:
NoSuchMessageException
- if not found
getErrors
@Nullable public Errors getErrors(String name)
Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.- 参数:
name
- the name of the bind object- 返回:
- the Errors instance, or
null
if not found
getErrors
@Nullable public Errors getErrors(String name, boolean htmlEscape)
Retrieve the Errors instance for the given bind object.- 参数:
name
- the name of the bind objecthtmlEscape
- create an Errors instance with automatic HTML escaping?- 返回:
- the Errors instance, or
null
if not found
getModelObject
@Nullable protected <T> T getModelObject(String modelName)
Retrieve the model object for the given model name, either from the model or from the request attributes.- 参数:
modelName
- the name of the model object- 返回:
- the model object
getBindStatus
public BindStatus getBindStatus(String path) throws IllegalStateException
Create a BindStatus for the given bind object using the "defaultHtmlEscape" setting.- 参数:
path
- the bean and property path for which values and errors will be resolved (e.g. "person.age")- 返回:
- the new BindStatus instance
- 抛出:
IllegalStateException
- if no corresponding Errors object found
getBindStatus
public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException
Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.- 参数:
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?- 返回:
- the new BindStatus instance
- 抛出:
IllegalStateException
- if no corresponding Errors object found