org.springframework.web.reactive.result.view

Class RedirectView

  • All Implemented Interfaces:
    Aware, BeanNameAware, InitializingBean, ApplicationContextAware, View


    public class RedirectView
    extends AbstractUrlBasedView
    View that redirects to an absolute or context relative URL. The URL may be a URI template in which case the URI template variables will be replaced with values from the model or with URI variables from the current request.

    By default HttpStatus.SEE_OTHER is used but alternate status codes may be via constructor or setters arguments.

    Since:
    5.0
    Author:
    Sebastien Deleuze, Rossen Stoyanchev
    • Constructor Detail

      • RedirectView

        public RedirectView()
        Constructor for use as a bean.
      • RedirectView

        public RedirectView(java.lang.String redirectUrl)
        Create a new RedirectView with the given redirect URL. Status code HttpStatus.SEE_OTHER is used by default.
    • Method Detail

      • setContextRelative

        public void setContextRelative(boolean contextRelative)
        Whether to interpret a given redirect URLs that starts with a slash ("/") as relative to the current context path (true, the default) or to the web server root (false).
      • isContextRelative

        public boolean isContextRelative()
        Whether to interpret URLs as relative to the current context path.
      • getStatusCode

        public HttpStatus getStatusCode()
        Get the redirect status code to use.
      • setPropagateQuery

        public void setPropagateQuery(boolean propagateQuery)
        Whether to append the query string of the current URL to the redirect URL (true) or not (false, the default).
      • isPropagateQuery

        public boolean isPropagateQuery()
        Whether the query string of the current URL is appended to the redirect URL.
      • setHosts

        public void setHosts(@Nullable
                             java.lang.String... hosts)
        Configure one or more hosts associated with the application. All other hosts will be considered external hosts.

        In effect this provides a way turn off encoding for URLs that have a host and that host is not listed as a known host.

        If not set (the default) all redirect URLs are encoded.

        Parameters:
        hosts - one or more application hosts
      • getHosts

        @Nullable
        public java.lang.String[] getHosts()
        Return the configured application hosts.
      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.Exception
        Description copied from interface: InitializingBean
        Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

        This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

        Specified by:
        afterPropertiesSet in interface InitializingBean
        Overrides:
        afterPropertiesSet in class AbstractUrlBasedView
        Throws:
        java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
      • isRedirectView

        public boolean isRedirectView()
        Description copied from interface: View
        Whether this View does rendering by performing a redirect.
      • checkResourceExists

        public boolean checkResourceExists(java.util.Locale locale)
                                    throws java.lang.Exception
        Description copied from class: AbstractUrlBasedView
        Check whether the resource for the configured URL actually exists.
        Specified by:
        checkResourceExists in class AbstractUrlBasedView
        Parameters:
        locale - the desired Locale that we're looking for
        Returns:
        false if the resource exists false if we know that it does not exist
        Throws:
        java.lang.Exception - if the resource exists but is invalid (e.g. could not be parsed)
      • renderInternal

        protected reactor.core.publisher.Mono<java.lang.Void> renderInternal(java.util.Map<java.lang.String,java.lang.Object> model,
                                                                             @Nullable
                                                                             MediaType contentType,
                                                                             ServerWebExchange exchange)
        Convert model to request parameters and redirect to the given URL.
        Specified by:
        renderInternal in class AbstractView
        Parameters:
        model - combined output Map (never null), with dynamic values taking precedence over static attributes
        contentType - the content type selected to render with which should match one of the supported media types.
        exchange - current exchange @return Mono to represent when and if rendering succeeds
      • createTargetUrl

        protected final java.lang.String createTargetUrl(java.util.Map<java.lang.String,java.lang.Object> model,
                                                         ServerWebExchange exchange)
        Create the target URL and, if necessary, pre-pend the contextPath, expand URI template variables, append the current request query, and apply the configured RequestDataValueProcessor.
      • expandTargetUrlTemplate

        protected java.lang.StringBuilder expandTargetUrlTemplate(java.lang.String targetUrl,
                                                                  java.util.Map<java.lang.String,java.lang.Object> model,
                                                                  java.util.Map<java.lang.String,java.lang.String> uriVariables)
        Expand URI template variables in the target URL with either model attribute values or as a fallback with URI variable values from the current request. Values are encoded.
      • appendCurrentRequestQuery

        protected java.lang.StringBuilder appendCurrentRequestQuery(java.lang.String targetUrl,
                                                                    ServerHttpRequest request)
        Append the query of the current request to the target redirect URL.
      • sendRedirect

        protected reactor.core.publisher.Mono<java.lang.Void> sendRedirect(java.lang.String targetUrl,
                                                                           ServerWebExchange exchange)
        Send a redirect back to the HTTP client.
        Parameters:
        targetUrl - the target URL to redirect to
        exchange - current exchange
      • isRemoteHost

        protected boolean isRemoteHost(java.lang.String targetUrl)
        Whether the given targetUrl has a host that is a "foreign" system in which case HttpServletResponse.encodeRedirectURL(java.lang.String) will not be applied. This method returns true if the setHosts(String[]) property is configured and the target URL has a host that does not match.
        Parameters:
        targetUrl - the target redirect URL
        Returns:
        true the target URL has a remote host, false if it the URL does not have a host or the "host" property is not configured.