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

public Class RedirectView

extends AbstractUrlBasedView
Class Inheritance
Imports
java.net.URI, java.nio.charset.StandardCharsets, java.util.Collections, .Locale, .Map, java.util.regex.Matcher, .Pattern, reactor.core.publisher.Mono, org.springframework.http.HttpStatus, .HttpStatusCode, .MediaType, org.springframework.http.server.reactive.ServerHttpRequest, .ServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils, .StringUtils, org.springframework.web.reactive.HandlerMapping, org.springframework.web.server.ServerWebExchange, org.springframework.web.util.UriComponentsBuilder, .UriUtils

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 supplied via constructor or setters arguments.

Authors
Sebastien Deleuze, Rossen Stoyanchev
Since
5.0

Field Summary

Modifier and TypeField and Description
private boolean
private String[]
private boolean
private HttpStatusCode
private static final Pattern

Constructor Summary

AccessConstructor and Description
public
RedirectView()

Constructor for use as a bean.

public
RedirectView(String redirectUrl)

Create a new RedirectView with the given redirect URL.

public
RedirectView(String redirectUrl, HttpStatusCode statusCode)

Create a new RedirectView with the given URL and an alternate redirect status code such as HttpStatus#TEMPORARY_REDIRECT or HttpStatus#PERMANENT_REDIRECT.

Method Summary

Modifier and TypeMethod and Description
protected StringBuilder
appendCurrentRequestQuery(String targetUrl, ServerHttpRequest request)

Append the query of the current request to the target redirect URL.

public boolean
checkResourceExists(Locale
the desired Locale that we're looking for
locale
)

Implements abstract org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

Check whether the resource for the configured URL actually exists.

protected final String
createTargetUrl(Map<String, 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.

private String
protected StringBuilder
expandTargetUrlTemplate(String targetUrl, Map<String, Object> model, Map<String, 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.

private Map<String, String>
public String[]
getHosts()

Return the configured application hosts.

public HttpStatusCode
getStatusCode()

Get the redirect status code to use.

public boolean
isContextRelative()

Whether to interpret URLs as relative to the current context path.

public boolean
isPropagateQuery()

Whether the query string of the current URL is appended to the redirect URL.

public boolean
isRedirectView()

Overrides default org.springframework.web.reactive.result.view.View.isRedirectView.

Whether this View does render by performing a redirect.

protected boolean

Returns:

true if the target URL has a remote host, false if the URL does not have a host or the "host" property is not configured
isRemoteHost
(String
the target redirect URL
targetUrl
)

Whether the given targetUrl has a host that is a "foreign" system in which case jakarta.servlet.http.HttpServletResponse#encodeRedirectURL will not be applied.

protected Mono<Void>
renderInternal(Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
model
,
MediaType
the content type selected to render with, which should match one of the supported media types
contentType
,
ServerWebExchange
current exchange
exchange
)

Implements abstract org.springframework.web.reactive.result.view.AbstractView.renderInternal.

Convert model to request parameters and redirect to the given URL.

protected Mono<Void>
sendRedirect(String
the target URL to redirect to
targetUrl
,
ServerWebExchange
current exchange
exchange
)

Send a redirect back to the HTTP client.

public void
setContextRelative(boolean contextRelative)

Whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current context path (true, the default) or relative to the web server root (false).

public void
setHosts(String...
one or more application hosts
hosts
)

Configure one or more hosts associated with the application.

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).

public void
setStatusCode(HttpStatusCode statusCode)

Set an alternate redirect status code such as HttpStatus#TEMPORARY_REDIRECT or HttpStatus#PERMANENT_REDIRECT.

Inherited from org.springframework.web.reactive.result.view.AbstractUrlBasedView:
afterPropertiesSetgetUrlresourceExistssetUrltoString

Field Detail

contextRelativeback to summary
private boolean contextRelative
hostsback to summary
private String[] hosts
Annotations
@Nullable
propagateQueryback to summary
private boolean propagateQuery
statusCodeback to summary
private HttpStatusCode statusCode
URI_TEMPLATE_VARIABLE_PATTERNback to summary
private static final Pattern URI_TEMPLATE_VARIABLE_PATTERN

Constructor Detail

RedirectViewback to summary
public RedirectView()

Constructor for use as a bean.

RedirectViewback to summary
public RedirectView(String redirectUrl)

Create a new RedirectView with the given redirect URL.

Status code HttpStatus#SEE_OTHER is used by default.

RedirectViewback to summary
public RedirectView(String redirectUrl, HttpStatusCode statusCode)

Create a new RedirectView with the given URL and an alternate redirect status code such as HttpStatus#TEMPORARY_REDIRECT or HttpStatus#PERMANENT_REDIRECT.

Method Detail

appendCurrentRequestQueryback to summary
protected StringBuilder appendCurrentRequestQuery(String targetUrl, ServerHttpRequest request)

Append the query of the current request to the target redirect URL.

checkResourceExistsback to summary
public boolean checkResourceExists(Locale locale) throws Exception

Implements abstract org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

Doc from org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

Check whether the resource for the configured URL actually exists.

Parameters
locale:Locale

the desired Locale that we're looking for

Returns:boolean

false if the resource exists false if we know that it does not exist

Annotations
@Override
Exceptions
Exception:
if the resource exists but is invalid (e.g. could not be parsed)
createTargetUrlback to summary
protected final String createTargetUrl(Map<String, 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.

encodeUriVariableback to summary
private String encodeUriVariable(String text)
expandTargetUrlTemplateback to summary
protected StringBuilder expandTargetUrlTemplate(String targetUrl, Map<String, Object> model, Map<String, 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.

getCurrentUriVariablesback to summary
private Map<String, String> getCurrentUriVariables(ServerWebExchange exchange)
getHostsback to summary
public String[] getHosts()

Return the configured application hosts.

Annotations
@Nullable
getStatusCodeback to summary
public HttpStatusCode getStatusCode()

Get the redirect status code to use.

isContextRelativeback to summary
public boolean isContextRelative()

Whether to interpret URLs as relative to the current context path.

isPropagateQueryback to summary
public boolean isPropagateQuery()

Whether the query string of the current URL is appended to the redirect URL.

isRedirectViewback to summary
public boolean isRedirectView()

Overrides default org.springframework.web.reactive.result.view.View.isRedirectView.

Doc from org.springframework.web.reactive.result.view.View.isRedirectView.

Whether this View does render by performing a redirect.

Annotations
@Override
isRemoteHostback to summary
protected boolean isRemoteHost(String targetUrl)

Whether the given targetUrl has a host that is a "foreign" system in which case jakarta.servlet.http.HttpServletResponse#encodeRedirectURL 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:String

the target redirect URL

Returns:boolean

true if the target URL has a remote host, false if the URL does not have a host or the "host" property is not configured

renderInternalback to summary
protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType, ServerWebExchange exchange)

Implements abstract org.springframework.web.reactive.result.view.AbstractView.renderInternal.

Convert model to request parameters and redirect to the given URL.

Parameters
model:Map<String, Object>

Doc from org.springframework.web.reactive.result.view.AbstractView.renderInternal.

combined output Map (never null), with dynamic values taking precedence over static attributes

contentType:MediaType

Doc from org.springframework.web.reactive.result.view.AbstractView.renderInternal.

the content type selected to render with, which should match one of the supported media types

exchange:ServerWebExchange

Doc from org.springframework.web.reactive.result.view.AbstractView.renderInternal.

current exchange

Returns:Mono<Void>

Doc from org.springframework.web.reactive.result.view.AbstractView.renderInternal.

a Mono that represents when and if rendering succeeds

Annotations
@Override
sendRedirectback to summary
protected Mono<Void> sendRedirect(String targetUrl, ServerWebExchange exchange)

Send a redirect back to the HTTP client.

Parameters
targetUrl:String

the target URL to redirect to

exchange:ServerWebExchange

current exchange

setContextRelativeback to summary
public void setContextRelative(boolean contextRelative)

Whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current context path (true, the default) or relative to the web server root (false).

setHostsback to summary
public void setHosts(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:String[]

one or more application hosts

setPropagateQueryback to summary
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).

setStatusCodeback to summary
public void setStatusCode(HttpStatusCode statusCode)

Set an alternate redirect status code such as HttpStatus#TEMPORARY_REDIRECT or HttpStatus#PERMANENT_REDIRECT.