public class RedirectView extends AbstractUrlBasedView
By default HttpStatus.SEE_OTHER
is used but alternate status
codes may be via constructor or setters arguments.
Modifier and Type | Field and Description |
---|---|
private boolean |
contextRelative |
private java.lang.String[] |
hosts |
private boolean |
propagateQuery |
private HttpStatus |
statusCode |
private static java.util.regex.Pattern |
URI_TEMPLATE_VARIABLE_PATTERN |
logger, REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
Constructor and Description |
---|
RedirectView()
Constructor for use as a bean.
|
RedirectView(java.lang.String redirectUrl)
Create a new
RedirectView with the given redirect URL. |
RedirectView(java.lang.String redirectUrl,
HttpStatus statusCode)
Create a new
RedirectView with the given URL and an alternate
redirect status code such as HttpStatus.TEMPORARY_REDIRECT or
HttpStatus.PERMANENT_REDIRECT . |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected java.lang.StringBuilder |
appendCurrentRequestQuery(java.lang.String targetUrl,
ServerHttpRequest request)
Append the query of the current request to the target redirect URL.
|
boolean |
checkResourceExists(java.util.Locale locale)
Check whether the resource for the configured URL actually exists.
|
protected 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 . |
private java.lang.String |
encodeUriVariable(java.lang.String text) |
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.
|
private java.util.Map<java.lang.String,java.lang.String> |
getCurrentUriVariables(ServerWebExchange exchange) |
java.lang.String[] |
getHosts()
Return the configured application hosts.
|
HttpStatus |
getStatusCode()
Get the redirect status code to use.
|
boolean |
isContextRelative()
Whether to interpret URLs as relative to the current context path.
|
boolean |
isPropagateQuery()
Whether the query string of the current URL is appended to the redirect URL.
|
boolean |
isRedirectView()
Whether this View does rendering by performing a redirect.
|
protected boolean |
isRemoteHost(java.lang.String targetUrl)
Whether the given targetUrl has a host that is a "foreign" system in which
case
javax.servlet.http.HttpServletResponse#encodeRedirectURL will not be applied. |
protected <any> |
renderInternal(java.util.Map<java.lang.String,java.lang.Object> model,
MediaType contentType,
ServerWebExchange exchange)
Convert model to request parameters and redirect to the given URL.
|
protected <any> |
sendRedirect(java.lang.String targetUrl,
ServerWebExchange exchange)
Send a redirect back to the HTTP client
|
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 ). |
void |
setHosts(java.lang.String... hosts)
Configure one or more hosts associated with the application.
|
void |
setPropagateQuery(boolean propagateQuery)
Whether to append the query string of the current URL to the redirect URL
(
true ) or not (false , the default). |
void |
setStatusCode(HttpStatus statusCode)
Set an alternate redirect status code such as
HttpStatus.TEMPORARY_REDIRECT or
HttpStatus.PERMANENT_REDIRECT . |
getUrl, setUrl, toString
createRequestContext, getApplicationContext, getDefaultCharset, getModelAttributes, getRequestContextAttribute, getRequestDataValueProcessor, getSupportedMediaTypes, obtainApplicationContext, render, resolveAsyncAttributes, setApplicationContext, setDefaultCharset, setRequestContextAttribute, setSupportedMediaTypes
private static final java.util.regex.Pattern URI_TEMPLATE_VARIABLE_PATTERN
private boolean contextRelative
private HttpStatus statusCode
private boolean propagateQuery
@Nullable private java.lang.String[] hosts
public RedirectView()
public RedirectView(java.lang.String redirectUrl)
RedirectView
with the given redirect URL.
Status code HttpStatus.SEE_OTHER
is used by default.public RedirectView(java.lang.String redirectUrl, HttpStatus statusCode)
RedirectView
with the given URL and an alternate
redirect status code such as HttpStatus.TEMPORARY_REDIRECT
or
HttpStatus.PERMANENT_REDIRECT
.public void setContextRelative(boolean contextRelative)
true
, the default) or to
the web server root (false
).public boolean isContextRelative()
public void setStatusCode(HttpStatus statusCode)
HttpStatus.TEMPORARY_REDIRECT
or
HttpStatus.PERMANENT_REDIRECT
.public HttpStatus getStatusCode()
public void setPropagateQuery(boolean propagateQuery)
true
) or not (false
, the default).public boolean isPropagateQuery()
public void setHosts(@Nullable java.lang.String... 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.
hosts
- one or more application hosts@Nullable public java.lang.String[] getHosts()
public void afterPropertiesSet() throws java.lang.Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
afterPropertiesSet
in class AbstractUrlBasedView
java.lang.Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.public boolean isRedirectView()
View
public boolean checkResourceExists(java.util.Locale locale) throws java.lang.Exception
AbstractUrlBasedView
checkResourceExists
in class AbstractUrlBasedView
locale
- the desired Locale that we're looking forfalse
if the resource exists
false
if we know that it does not existjava.lang.Exception
- if the resource exists but is invalid (e.g. could not be parsed)protected <any> renderInternal(java.util.Map<java.lang.String,java.lang.Object> model, @Nullable MediaType contentType, ServerWebExchange exchange)
renderInternal
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributescontentType
- 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 succeedsprotected final java.lang.String createTargetUrl(java.util.Map<java.lang.String,java.lang.Object> model, ServerWebExchange exchange)
RequestDataValueProcessor
.private java.util.Map<java.lang.String,java.lang.String> getCurrentUriVariables(ServerWebExchange exchange)
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)
private java.lang.String encodeUriVariable(java.lang.String text)
protected java.lang.StringBuilder appendCurrentRequestQuery(java.lang.String targetUrl, ServerHttpRequest request)
protected <any> sendRedirect(java.lang.String targetUrl, ServerWebExchange exchange)
targetUrl
- the target URL to redirect toexchange
- current exchangeprotected boolean isRemoteHost(java.lang.String targetUrl)
javax.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.targetUrl
- the target redirect URLtrue
the target URL has a remote host, false
if it
the URL does not have a host or the "host" property is not configured.