Top Description Fields Constructors Methods
org.springframework.web.servlet.view

public Class RedirectView

extends AbstractUrlBasedView
implements SmartView
Class Inheritance
All Implemented Interfaces
org.springframework.web.servlet.SmartView, org.springframework.web.servlet.View
Imports
java.io.IOException, .UnsupportedEncodingException, java.lang.reflect.Array, java.net.URLEncoder, java.util.Collection, .Collections, .LinkedHashMap, .Map, java.util.regex.Matcher, .Pattern, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.beans.BeanUtils, org.springframework.http.HttpStatus, .HttpStatusCode, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .ObjectUtils, .StringUtils, org.springframework.web.context.WebApplicationContext, org.springframework.web.servlet.HandlerMapping, .SmartView, .View, org.springframework.web.servlet.support.RequestContextUtils, .RequestDataValueProcessor, org.springframework.web.util.UriComponentsBuilder, .UriUtils, .WebUtils

View that redirects to an absolute, context relative, or current request relative URL. The URL may be a URI template in which case the URI template variables will be replaced with values available in the model. By default, all primitive model attributes (or collections thereof) are exposed as HTTP query parameters (assuming they've not been used as URI template variables), but this behavior can be changed by overriding the isEligibleProperty(String, Object) method.

A URL for this view is supposed to be an HTTP redirect URL, i.e. suitable for HttpServletResponse's sendRedirect method, which is what actually does the redirect if the HTTP 1.0 flag is on, or via sending back an HTTP 303 code - if the HTTP 1.0 compatibility flag is off.

Note that while the default value for the "contextRelative" flag is off, you will probably want to almost always set it to true. With the flag off, URLs starting with "/" are considered relative to the web server root, while with the flag on, they are considered relative to the web application root. Since most web applications will never know or care what their context path actually is, they are much better off setting this flag to true, and submitting paths which are to be considered relative to the web application root.

NOTE when using this redirect view in a Portlet environment: Make sure that your controller respects the Portlet sendRedirect constraints.

Authors
Rod Johnson, Juergen Hoeller, Colin Sampaleanu, Sam Brannen, Arjen Poutsma, Rossen Stoyanchev
See Also
setContextRelative, setHttp10Compatible, setExposeModelAttributes, jakarta.servlet.http.HttpServletResponse#sendRedirect

Field Summary

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

Constructor Summary

AccessConstructor and Description
public
RedirectView()

Constructor for use as a bean.

public
RedirectView(String
the URL to redirect to
url
)

Create a new RedirectView with the given URL.

public
RedirectView(String
the URL to redirect to
url
,
boolean
whether to interpret the given URL as relative to the current ServletContext
contextRelative
)

Create a new RedirectView with the given URL.

public
RedirectView(String
the URL to redirect to
url
,
boolean
whether to interpret the given URL as relative to the current ServletContext
contextRelative
,
boolean
whether to stay compatible with HTTP 1.0 clients
http10Compatible
)

Create a new RedirectView with the given URL.

public
RedirectView(String
the URL to redirect to
url
,
boolean
whether to interpret the given URL as relative to the current ServletContext
contextRelative
,
boolean
whether to stay compatible with HTTP 1.0 clients
http10Compatible
,
boolean
whether model attributes should be exposed as query parameters
exposeModelAttributes
)

Create a new RedirectView with the given URL.

Method Summary

Modifier and TypeMethod and Description
protected void
appendCurrentQueryParams(StringBuilder
the StringBuilder to append the properties to
targetUrl
,
HttpServletRequest
the current request
request
)

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

protected void
appendQueryProperties(StringBuilder
the StringBuilder to append the properties to
targetUrl
,
Map<String, Object>
a Map that contains model attributes
model
,
String
the encoding scheme to use
encodingScheme
)

Append query properties to the redirect URL.

protected final String
createTargetUrl(Map<String, Object> model, HttpServletRequest request)

Create the target URL by checking if the redirect string is a URI template first, expanding it with the given model, and then optionally appending simple type model attributes as query String parameters.

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

Return the configured application hosts.

protected HttpStatusCode

Returns:

the response status
getHttp11StatusCode
(HttpServletRequest
the request to inspect
request
,
HttpServletResponse
the servlet response
response
,
String
the target URL
targetUrl
)

Determines the status code to use for HTTP 1.1 compatible requests.

protected boolean
isContextRequired()

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.isContextRequired.

An ApplicationContext is not strictly required for RedirectView.

protected boolean

Returns:

whether the element is eligible as query property
isEligibleProperty
(String
the key of the model element
key
,
Object
the value of the model element
value
)

Determine whether the given model element should be exposed as a query property.

protected boolean

Returns:

whether the element value is eligible
isEligibleValue
(Object
the model element value
value
)

Determine whether the given model element value is eligible for exposure.

public boolean
isPropagateQueryProperties()

Whether to propagate the query params of the current URL.

public boolean
isRedirectView()

Implements org.springframework.web.servlet.SmartView.isRedirectView.

Returns "true" indicating this view performs 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 HttpServletResponse#encodeRedirectURL will not be applied.

protected Map<String, Object>

Returns:

the filtered Map of eligible query properties
queryProperties
(Map<String, Object>
the original model Map
model
)

Determine name-value pairs for query strings, which will be stringified, URL-encoded and formatted by appendQueryProperties.

protected void
renderMergedOutputModel(Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

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

protected StringBuilder
replaceUriTemplateVariables(String
the redirect URL
targetUrl
,
Map<String, Object>
a Map that contains model attributes
model
,
Map<String, String>
current request URI variables to use
currentUriVariables
,
String
the encoding scheme to use
encodingScheme
)

Replace URI template variables in the target URL with encoded model attributes or URI variables from the current request.

protected void
sendRedirect(HttpServletRequest
current HTTP request (allows for reacting to request method)
request
,
HttpServletResponse
current HTTP response (for sending response headers)
response
,
String
the target URL to redirect to
targetUrl
,
boolean
whether to stay compatible with HTTP 1.0 clients
http10Compatible
)

Send a redirect back to the HTTP client.

public void
setContextRelative(boolean contextRelative)

Set whether to interpret a given URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.

public void
setEncodingScheme(String encodingScheme)

Set the encoding scheme for this view.

public void
setExpandUriTemplateVariables(boolean expandUriTemplateVariables)

Whether to treat the redirect URL as a URI template.

public void
setExposeModelAttributes(final boolean exposeModelAttributes)

Set the exposeModelAttributes flag which denotes whether model attributes should be exposed as HTTP query parameters.

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

Configure one or more hosts associated with the application.

public void
setHttp10Compatible(boolean http10Compatible)

Set whether to stay compatible with HTTP 1.0 clients.

public void
setPropagateQueryParams(boolean propagateQueryParams)

When set to true the query string of the current URL is appended and thus propagated through to the redirected URL.

public void
setStatusCode(HttpStatusCode statusCode)

Set the status code for this view.

protected String

Returns:

the updated URL or the same as URL as the one passed in
updateTargetUrl
(String
the given redirect URL
targetUrl
,
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)

Find the registered RequestDataValueProcessor, if any, and allow it to update the redirect target URL.

protected String

Returns:

the encoded output String
urlEncode
(String
the unencoded input String
input
,
String
the encoding scheme
encodingScheme
)

URL-encode the given input String with the given encoding scheme.

Inherited from org.springframework.web.servlet.view.AbstractUrlBasedView:
afterPropertiesSetcheckResourcegetUrlisUrlRequiredsetUrltoString

Field Detail

contextRelativeback to summary
private boolean contextRelative
encodingSchemeback to summary
private String encodingScheme
Annotations
@Nullable
expandUriTemplateVariablesback to summary
private boolean expandUriTemplateVariables
exposeModelAttributesback to summary
private boolean exposeModelAttributes
hostsback to summary
private String[] hosts
Annotations
@Nullable
http10Compatibleback to summary
private boolean http10Compatible
propagateQueryParamsback to summary
private boolean propagateQueryParams
statusCodeback to summary
private HttpStatusCode statusCode
Annotations
@Nullable
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 url)

Create a new RedirectView with the given URL.

The given URL will be considered as relative to the web server, not as relative to the current ServletContext.

Parameters
url:String

the URL to redirect to

See Also
RedirectView(String, boolean)
RedirectViewback to summary
public RedirectView(String url, boolean contextRelative)

Create a new RedirectView with the given URL.

Parameters
url:String

the URL to redirect to

contextRelative:boolean

whether to interpret the given URL as relative to the current ServletContext

RedirectViewback to summary
public RedirectView(String url, boolean contextRelative, boolean http10Compatible)

Create a new RedirectView with the given URL.

Parameters
url:String

the URL to redirect to

contextRelative:boolean

whether to interpret the given URL as relative to the current ServletContext

http10Compatible:boolean

whether to stay compatible with HTTP 1.0 clients

RedirectViewback to summary
public RedirectView(String url, boolean contextRelative, boolean http10Compatible, boolean exposeModelAttributes)

Create a new RedirectView with the given URL.

Parameters
url:String

the URL to redirect to

contextRelative:boolean

whether to interpret the given URL as relative to the current ServletContext

http10Compatible:boolean

whether to stay compatible with HTTP 1.0 clients

exposeModelAttributes:boolean

whether model attributes should be exposed as query parameters

Method Detail

appendCurrentQueryParamsback to summary
protected void appendCurrentQueryParams(StringBuilder targetUrl, HttpServletRequest request)

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

Parameters
targetUrl:StringBuilder

the StringBuilder to append the properties to

request:HttpServletRequest

the current request

Since
4.1
appendQueryPropertiesback to summary
protected void appendQueryProperties(StringBuilder targetUrl, Map<String, Object> model, String encodingScheme) throws UnsupportedEncodingException

Append query properties to the redirect URL. Stringifies, URL-encodes and formats model attributes as query properties.

Parameters
targetUrl:StringBuilder

the StringBuilder to append the properties to

model:Map<String, Object>

a Map that contains model attributes

encodingScheme:String

the encoding scheme to use

Annotations
@SuppressWarnings:unchecked
Exceptions
UnsupportedEncodingException:
if string encoding failed
See Also
queryProperties
createTargetUrlback to summary
protected final String createTargetUrl(Map<String, Object> model, HttpServletRequest request) throws UnsupportedEncodingException

Create the target URL by checking if the redirect string is a URI template first, expanding it with the given model, and then optionally appending simple type model attributes as query String parameters.

getContextPathback to summary
private String getContextPath(HttpServletRequest request)
getCurrentRequestUriVariablesback to summary
private Map<String, String> getCurrentRequestUriVariables(HttpServletRequest request)
Annotations
@SuppressWarnings:unchecked
getHostsback to summary
public String[] getHosts()

Return the configured application hosts.

Annotations
@Nullable
Since
4.3
getHttp11StatusCodeback to summary
protected HttpStatusCode getHttp11StatusCode(HttpServletRequest request, HttpServletResponse response, String targetUrl)

Determines the status code to use for HTTP 1.1 compatible requests.

The default implementation returns the statusCode property if set, or the value of the RESPONSE_STATUS_ATTRIBUTE attribute. If neither are set, it defaults to HttpStatus#SEE_OTHER (303).

Parameters
request:HttpServletRequest

the request to inspect

response:HttpServletResponse

the servlet response

targetUrl:String

the target URL

Returns:HttpStatusCode

the response status

isContextRequiredback to summary
protected boolean isContextRequired()

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.isContextRequired.

An ApplicationContext is not strictly required for RedirectView.

Annotations
@Override
isEligiblePropertyback to summary
protected boolean isEligibleProperty(String key, Object value)

Determine whether the given model element should be exposed as a query property.

The default implementation considers Strings and primitives as eligible, and also arrays and Collections/Iterables with corresponding elements. This can be overridden in subclasses.

Parameters
key:String

the key of the model element

value:Object

the value of the model element

Returns:boolean

whether the element is eligible as query property

isEligibleValueback to summary
protected boolean isEligibleValue(Object value)

Determine whether the given model element value is eligible for exposure.

The default implementation considers primitives, strings, numbers, dates, URIs, URLs etc as eligible, according to BeanUtils#isSimpleValueType. This can be overridden in subclasses.

Parameters
value:Object

the model element value

Returns:boolean

whether the element value is eligible

See Also
BeanUtils#isSimpleValueType
isPropagateQueryPropertiesback to summary
public boolean isPropagateQueryProperties()

Whether to propagate the query params of the current URL.

Since
4.1
isRedirectViewback to summary
public boolean isRedirectView()

Implements org.springframework.web.servlet.SmartView.isRedirectView.

Returns "true" indicating this view performs 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 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

Since
4.3
queryPropertiesback to summary
protected Map<String, Object> queryProperties(Map<String, Object> model)

Determine name-value pairs for query strings, which will be stringified, URL-encoded and formatted by appendQueryProperties.

This implementation filters the model through checking isEligibleProperty(String, Object) for each element, by default accepting Strings, primitives and primitive wrappers only.

Parameters
model:Map<String, Object>

the original model Map

Returns:Map<String, Object>

the filtered Map of eligible query properties

See Also
isEligibleProperty(String, Object)
renderMergedOutputModelback to summary
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws IOException

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

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

Parameters
model:Map<String, Object>

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

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

request:HttpServletRequest

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

current HTTP response

Annotations
@Override
See Also
appendQueryProperties, sendRedirect
replaceUriTemplateVariablesback to summary
protected StringBuilder replaceUriTemplateVariables(String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme) throws UnsupportedEncodingException

Replace URI template variables in the target URL with encoded model attributes or URI variables from the current request. Model attributes referenced in the URL are removed from the model.

Parameters
targetUrl:String

the redirect URL

model:Map<String, Object>

a Map that contains model attributes

currentUriVariables:Map<String, String>

current request URI variables to use

encodingScheme:String

the encoding scheme to use

Exceptions
UnsupportedEncodingException:
if string encoding failed
sendRedirectback to summary
protected void sendRedirect(HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean http10Compatible) throws IOException

Send a redirect back to the HTTP client.

Parameters
request:HttpServletRequest

current HTTP request (allows for reacting to request method)

response:HttpServletResponse

current HTTP response (for sending response headers)

targetUrl:String

the target URL to redirect to

http10Compatible:boolean

whether to stay compatible with HTTP 1.0 clients

Exceptions
IOException:
if thrown by response methods
setContextRelativeback to summary
public void setContextRelative(boolean contextRelative)

Set whether to interpret a given URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.

Default is "false": A URL that starts with a slash will be interpreted as absolute, i.e. taken as-is. If "true", the context path will be prepended to the URL in such a case.

See Also
jakarta.servlet.http.HttpServletRequest#getContextPath
setEncodingSchemeback to summary
public void setEncodingScheme(String encodingScheme)

Set the encoding scheme for this view.

Default is the request's encoding scheme (which is ISO-8859-1 if not specified otherwise).

setExpandUriTemplateVariablesback to summary
public void setExpandUriTemplateVariables(boolean expandUriTemplateVariables)

Whether to treat the redirect URL as a URI template. Set this flag to false if the redirect URL contains open and close curly braces "{", "}" and you don't want them interpreted as URI variables.

Defaults to true.

setExposeModelAttributesback to summary
public void setExposeModelAttributes(final boolean exposeModelAttributes)

Set the exposeModelAttributes flag which denotes whether model attributes should be exposed as HTTP query parameters.

Defaults to true.

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 property provides a way turn off encoding via HttpServletResponse#encodeRedirectURL for URLs that have a host and that host is not listed as a known host.

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

Parameters
hosts:String[]

one or more application hosts

Since
4.3
setHttp10Compatibleback to summary
public void setHttp10Compatible(boolean http10Compatible)

Set whether to stay compatible with HTTP 1.0 clients.

In the default implementation, this will enforce HTTP status code 302 in any case, i.e. delegate to HttpServletResponse.sendRedirect. Turning this off will send HTTP status code 303, which is the correct code for HTTP 1.1 clients, but not understood by HTTP 1.0 clients.

Many HTTP 1.1 clients treat 302 just like 303, not making any difference. However, some clients depend on 303 when redirecting after a POST request; turn this flag off in such a scenario.

See Also
jakarta.servlet.http.HttpServletResponse#sendRedirect
setPropagateQueryParamsback to summary
public void setPropagateQueryParams(boolean propagateQueryParams)

When set to true the query string of the current URL is appended and thus propagated through to the redirected URL.

Defaults to false.

Since
4.1
setStatusCodeback to summary
public void setStatusCode(HttpStatusCode statusCode)

Set the status code for this view.

Default is to send 302/303, depending on the value of the http10Compatible flag.

updateTargetUrlback to summary
protected String updateTargetUrl(String targetUrl, Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)

Find the registered RequestDataValueProcessor, if any, and allow it to update the redirect target URL.

Parameters
targetUrl:String

the given redirect URL

Returns:String

the updated URL or the same as URL as the one passed in

urlEncodeback to summary
protected String urlEncode(String input, String encodingScheme) throws UnsupportedEncodingException

URL-encode the given input String with the given encoding scheme.

The default implementation uses URLEncoder.encode(input, enc).

Parameters
input:String

the unencoded input String

encodingScheme:String

the encoding scheme

Returns:String

the encoded output String

Exceptions
UnsupportedEncodingException:
if thrown by the JDK URLEncoder
See Also
java.net.URLEncoder#encode(String, String)