org.springframework.web.reactive.result.view

Class RequestContext

  • java.lang.Object
    • org.springframework.web.reactive.result.view.RequestContext


  • public class RequestContext
    extends java.lang.Object
    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 model attribute via AbstractView's "requestContextAttribute" property.

    Since:
    5.0
    Author:
    Rossen Stoyanchev
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getMessageSource

        public MessageSource getMessageSource()
        Return the MessageSource in use with this request.
      • getModel

        @Nullable
        public java.util.Map<java.lang.String,java.lang.Object> getModel()
        Return the model Map that this RequestContext encapsulates, if any.
        Returns:
        the populated model Map, or null if none available
      • getLocale

        public final java.util.Locale getLocale()
        Return the current Locale.
      • getTimeZone

        public java.util.TimeZone getTimeZone()
        Return the current TimeZone.
      • changeLocale

        public void changeLocale(java.util.Locale locale)
        Change the current locale to the specified one.
      • changeLocale

        public void changeLocale(java.util.Locale locale,
                                 java.util.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 to false in case of no explicit default given.
      • getDefaultHtmlEscape

        @Nullable
        public java.lang.Boolean getDefaultHtmlEscape()
        Return the default HTML escape setting, differentiating between no default specified and an explicit value.
        Returns:
        whether default HTML escaping is enabled (null = no explicit default)
      • getContextPath

        public java.lang.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 java.lang.String getContextUrl(java.lang.String relativeUrl)
        Return a context-aware URl for the given relative URL.
        Parameters:
        relativeUrl - the relative URL part
        Returns:
        a URL that points back to the current web application with an absolute path also URL-encoded accordingly
      • getContextUrl

        public 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 {}. 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 - the relative URL part
        params - a map of parameters to insert as placeholders in the url
        Returns:
        a URL that points back to the current web application with an absolute path also URL-encoded accordingly
      • getRequestPath

        public java.lang.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 java.lang.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 java.lang.String getMessage(java.lang.String code,
                                           java.lang.String defaultMessage)
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        Parameters:
        code - code of the message
        defaultMessage - the String to return if the lookup fails
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           java.lang.String defaultMessage)
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        defaultMessage - the String to return if the lookup fails
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.util.List<?> args,
                                           java.lang.String defaultMessage)
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        Parameters:
        code - code of the message
        args - arguments for the message as a List, or null if none
        defaultMessage - the String to return if the lookup fails
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           java.lang.String defaultMessage,
                                           boolean htmlEscape)
        Retrieve the message for the given code.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        defaultMessage - the String to return if the lookup fails
        htmlEscape - if the message should be HTML-escaped
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code)
                                    throws NoSuchMessageException
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        Parameters:
        code - code of the message
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args)
                                    throws NoSuchMessageException
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.util.List<?> args)
                                    throws NoSuchMessageException
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        Parameters:
        code - code of the message
        args - arguments for the message as a List, or null if none
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           boolean htmlEscape)
                                    throws NoSuchMessageException
        Retrieve the message for the given code.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        htmlEscape - if the message should be HTML-escaped
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(MessageSourceResolvable resolvable)
                                    throws NoSuchMessageException
        Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the "defaultHtmlEscape" setting.
        Parameters:
        resolvable - the MessageSourceResolvable
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(MessageSourceResolvable resolvable,
                                           boolean htmlEscape)
                                    throws NoSuchMessageException
        Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).
        Parameters:
        resolvable - the MessageSourceResolvable
        htmlEscape - if the message should be HTML-escaped
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getErrors

        @Nullable
        public Errors getErrors(java.lang.String name)
        Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.
        Parameters:
        name - name of the bind object
        Returns:
        the Errors instance, or null if not found
      • getErrors

        @Nullable
        public Errors getErrors(java.lang.String name,
                                          boolean htmlEscape)
        Retrieve the Errors instance for the given bind object.
        Parameters:
        name - name of the bind object
        htmlEscape - create an Errors instance with automatic HTML escaping?
        Returns:
        the Errors instance, or null if not found
      • getModelObject

        @Nullable
        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.
        Parameters:
        modelName - the name of the model object
        Returns:
        the model object
      • getBindStatus

        public BindStatus getBindStatus(java.lang.String path)
                                 throws java.lang.IllegalStateException
        Create a BindStatus for the given bind object using the "defaultHtmlEscape" setting.
        Parameters:
        path - the bean and property path for which values and errors will be resolved (e.g. "person.age")
        Returns:
        the new BindStatus instance
        Throws:
        java.lang.IllegalStateException - if no corresponding Errors object found
      • getBindStatus

        public BindStatus getBindStatus(java.lang.String path,
                                        boolean htmlEscape)
                                 throws java.lang.IllegalStateException
        Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.
        Parameters:
        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?
        Returns:
        the new BindStatus instance
        Throws:
        java.lang.IllegalStateException - if no corresponding Errors object found