Top Description Fields Constructors Methods
org.springframework.web.reactive.result.view

public Class RequestContext

extends Object
Class Inheritance
Imports
java.util.HashMap, .List, .Locale, .Map, .TimeZone, org.springframework.context.MessageSource, .MessageSourceResolvable, .NoSuchMessageException, org.springframework.context.i18n.LocaleContext, .TimeZoneAwareLocaleContext, org.springframework.http.server.reactive.ServerHttpRequest, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils, org.springframework.validation.BindException, .BindingResult, .Errors, org.springframework.web.bind.EscapedErrors, org.springframework.web.server.ServerWebExchange, org.springframework.web.util.HtmlUtils, .UriComponentsBuilder

Context holder for request-specific state, like the 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.

Author
Rossen Stoyanchev
Since
5.0

Field Summary

Modifier and TypeField and Description
private final RequestDataValueProcessor
private Boolean
private Map<String, Errors>
private final ServerWebExchange
private Locale
private final MessageSource
private final Map<String, Object>
private TimeZone

Constructor Summary

AccessConstructor and Description
public
RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource)

public
RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource, RequestDataValueProcessor dataValueProcessor)

Method Summary

Modifier and TypeMethod and Description
public void
changeLocale(Locale locale)

Change the current locale to the specified one.

public void
changeLocale(Locale locale, TimeZone timeZone)

Change the current locale to the specified locale and time zone context.

public BindStatus

Returns:

the new BindStatus instance
getBindStatus
(String
the bean and property path for which values and errors will be resolved (e.g. "person.age")
path
)

Create a BindStatus for the given bind object using the "defaultHtmlEscape" setting.

public BindStatus

Returns:

the new BindStatus instance
getBindStatus
(String
the bean and property path for which values and errors will be resolved (e.g. "person.age")
path
,
boolean
create a BindStatus with automatic HTML escaping?
htmlEscape
)

Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.

public String
getContextPath()

Return the context path of the current web application.

public String

Returns:

a URL that points back to the current web application with an absolute path also URL-encoded accordingly
getContextUrl
(String
the relative URL part
relativeUrl
)

Return a context-aware URl for the given relative URL.

public String

Returns:

a URL that points back to the current web application with an absolute path also URL-encoded accordingly
getContextUrl
(String
the relative URL part
relativeUrl
,
Map<String, ?>
a map of parameters to insert as placeholders in the url
params
)

Return a context-aware URl for the given relative URL with placeholders -- named keys with braces {}.

public Boolean

Returns:

whether default HTML escaping is enabled (null = no explicit default)
getDefaultHtmlEscape
()

Return the default HTML escape setting, differentiating between no default specified and an explicit value.

public Errors

Returns:

the Errors instance, or null if not found
getErrors
(String
the name of the bind object
name
)

Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.

public Errors

Returns:

the Errors instance, or null if not found
getErrors
(String
the name of the bind object
name
,
boolean
create an Errors instance with automatic HTML escaping?
htmlEscape
)

Retrieve the Errors instance for the given bind object.

protected final ServerWebExchange
public final Locale
getLocale()

Return the current Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
List<?>
arguments for the message as a List, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
,
boolean
if the message should be HTML-escaped
htmlEscape
)

Retrieve the message for the given code.

public String

Returns:

the message
getMessage
(String
the code of the message
code
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
List<?>
arguments for the message as a List, or null if none
args
)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
boolean
if the message should be HTML-escaped
htmlEscape
)

Retrieve the message for the given code.

public String

Returns:

the message
getMessage
(MessageSourceResolvable
the MessageSourceResolvable
resolvable
)

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the "defaultHtmlEscape" setting.

public String

Returns:

the message
getMessage
(MessageSourceResolvable
the MessageSourceResolvable
resolvable
,
boolean
if the message should be HTML-escaped
htmlEscape
)

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).

public MessageSource
getMessageSource()

Return the MessageSource in use with this request.

public Map<String, Object>

Returns:

the populated model Map, or null if none available
getModel
()

Return the model Map that this RequestContext encapsulates, if any.

protected <T> T

Returns:

the model object
getModelObject
(String
the name of the model object
modelName
)

Retrieve the model object for the given model name, either from the model or from the request attributes.

public String
getQueryString()

Return the query string of the current request.

public RequestDataValueProcessor
getRequestDataValueProcessor()

Return the RequestDataValueProcessor instance to apply to in form tag libraries and to redirect URLs.

public String
getRequestPath()

Return the request path of the request.

public TimeZone
getTimeZone()

Return the current TimeZone.

public boolean
isDefaultHtmlEscape()

Is default HTML escaping active?

public void
setDefaultHtmlEscape(boolean defaultHtmlEscape)

(De)activate default HTML escaping for messages and errors, for the scope of this RequestContext.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataValueProcessorback to summary
private final RequestDataValueProcessor dataValueProcessor
Annotations
@Nullable
defaultHtmlEscapeback to summary
private Boolean defaultHtmlEscape
Annotations
@Nullable
errorsMapback to summary
private Map<String, Errors> errorsMap
Annotations
@Nullable
exchangeback to summary
private final ServerWebExchange exchange
localeback to summary
private Locale locale
messageSourceback to summary
private final MessageSource messageSource
modelback to summary
private final Map<String, Object> model
timeZoneback to summary
private TimeZone timeZone

Constructor Detail

RequestContextback to summary
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource)
RequestContextback to summary
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource, RequestDataValueProcessor dataValueProcessor)

Method Detail

changeLocaleback to summary
public void changeLocale(Locale locale)

Change the current locale to the specified one.

changeLocaleback to summary
public void changeLocale(Locale locale, TimeZone timeZone)

Change the current locale to the specified locale and time zone context.

getBindStatusback to summary
public BindStatus getBindStatus(String path) throws IllegalStateException

Create a BindStatus for the given bind object using the "defaultHtmlEscape" setting.

Parameters
path:String

the bean and property path for which values and errors will be resolved (e.g. "person.age")

Returns:BindStatus

the new BindStatus instance

Exceptions
IllegalStateException:
if no corresponding Errors object found
getBindStatusback to summary
public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException

Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.

Parameters
path:String

the bean and property path for which values and errors will be resolved (e.g. "person.age")

htmlEscape:boolean

create a BindStatus with automatic HTML escaping?

Returns:BindStatus

the new BindStatus instance

Exceptions
IllegalStateException:
if no corresponding Errors object found
getContextPathback to summary
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().

getContextUrlback to summary
public String getContextUrl(String relativeUrl)

Return a context-aware URl for the given relative URL.

Parameters
relativeUrl:String

the relative URL part

Returns:String

a URL that points back to the current web application with an absolute path also URL-encoded accordingly

getContextUrlback to summary
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 URL foo/{bar}?spam={spam} and a parameter map {bar=baz,spam=nuts} and the result will be [contextpath]/foo/baz?spam=nuts.

Parameters
relativeUrl:String

the relative URL part

params:Map<String, ?>

a map of parameters to insert as placeholders in the url

Returns:String

a URL that points back to the current web application with an absolute path also URL-encoded accordingly

getDefaultHtmlEscapeback to summary
public Boolean getDefaultHtmlEscape()

Return the default HTML escape setting, differentiating between no default specified and an explicit value.

Returns:Boolean

whether default HTML escaping is enabled (null = no explicit default)

Annotations
@Nullable
getErrorsback to summary
public Errors getErrors(String name)

Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.

Parameters
name:String

the name of the bind object

Returns:Errors

the Errors instance, or null if not found

Annotations
@Nullable
getErrorsback to summary
public Errors getErrors(String name, boolean htmlEscape)

Retrieve the Errors instance for the given bind object.

Parameters
name:String

the name of the bind object

htmlEscape:boolean

create an Errors instance with automatic HTML escaping?

Returns:Errors

the Errors instance, or null if not found

Annotations
@Nullable
getExchangeback to summary
protected final ServerWebExchange getExchange()
getLocaleback to summary
public final Locale getLocale()

Return the current Locale.

getMessageback to summary
public String getMessage(String code, String defaultMessage)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

Parameters
code:String

the code of the message

defaultMessage:String

the String to return if the lookup fails

Returns:String

the message

getMessageback to summary
public String getMessage(String code, Object[] args, String defaultMessage)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

defaultMessage:String

the String to return if the lookup fails

Returns:String

the message

getMessageback to summary
public String getMessage(String code, List<?> args, String defaultMessage)

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

Parameters
code:String

the code of the message

args:List<?>

arguments for the message as a List, or null if none

defaultMessage:String

the String to return if the lookup fails

Returns:String

the message

getMessageback to summary
public String getMessage(String code, Object[] args, String defaultMessage, boolean htmlEscape)

Retrieve the message for the given code.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

defaultMessage:String

the String to return if the lookup fails

htmlEscape:boolean

if the message should be HTML-escaped

Returns:String

the message

getMessageback to summary
public String getMessage(String code) throws NoSuchMessageException

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

Parameters
code:String

the code of the message

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(String code, Object[] args) throws NoSuchMessageException

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(String code, List<?> args) throws NoSuchMessageException

Retrieve the message for the given code, using the "defaultHtmlEscape" setting.

Parameters
code:String

the code of the message

args:List<?>

arguments for the message as a List, or null if none

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(String code, Object[] args, boolean htmlEscape) throws NoSuchMessageException

Retrieve the message for the given code.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

htmlEscape:boolean

if the message should be HTML-escaped

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the "defaultHtmlEscape" setting.

Parameters
resolvable:MessageSourceResolvable

the MessageSourceResolvable

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).

Parameters
resolvable:MessageSourceResolvable

the MessageSourceResolvable

htmlEscape:boolean

if the message should be HTML-escaped

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageSourceback to summary
public MessageSource getMessageSource()

Return the MessageSource in use with this request.

getModelback to summary
public Map<String, Object> getModel()

Return the model Map that this RequestContext encapsulates, if any.

Returns:Map<String, Object>

the populated model Map, or null if none available

Annotations
@Nullable
getModelObjectback to summary
protected <T> T getModelObject(String modelName)

Retrieve the model object for the given model name, either from the model or from the request attributes.

Parameters
modelName:String

the name of the model object

Returns:T

the model object

Annotations
@SuppressWarnings:unchecked
@Nullable
getQueryStringback to summary
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.

getRequestDataValueProcessorback to summary
public RequestDataValueProcessor getRequestDataValueProcessor()

Return the RequestDataValueProcessor instance to apply to in form tag libraries and to redirect URLs.

Annotations
@Nullable
getRequestPathback to summary
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.

getTimeZoneback to summary
public TimeZone getTimeZone()

Return the current TimeZone.

isDefaultHtmlEscapeback to summary
public boolean isDefaultHtmlEscape()

Is default HTML escaping active? Falls back to false in case of no explicit default given.

setDefaultHtmlEscapeback to summary
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 ...