public class RedirectView extends AbstractUrlBasedView implements SmartView
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 a 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.
setContextRelative(boolean)
,
setHttp10Compatible(boolean)
,
setExposeModelAttributes(boolean)
,
javax.servlet.http.HttpServletResponse#sendRedirect
Modifier and Type | Field and Description |
---|---|
private boolean |
contextRelative |
private java.lang.String |
encodingScheme |
private boolean |
expandUriTemplateVariables |
private boolean |
exposeModelAttributes |
private boolean |
http10Compatible |
private HttpStatus |
statusCode |
private static java.util.regex.Pattern |
URI_TEMPLATE_VARIABLE_PATTERN |
DEFAULT_CONTENT_TYPE
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
RedirectView()
Constructor for use as a bean.
|
RedirectView(java.lang.String url)
Create a new RedirectView with the given URL.
|
RedirectView(java.lang.String url,
boolean contextRelative)
Create a new RedirectView with the given URL.
|
RedirectView(java.lang.String url,
boolean contextRelative,
boolean http10Compatible)
Create a new RedirectView with the given URL.
|
RedirectView(java.lang.String url,
boolean contextRelative,
boolean http10Compatible,
boolean exposeModelAttributes)
Create a new RedirectView with the given URL.
|
Modifier and Type | Method and Description |
---|---|
protected void |
appendQueryProperties(java.lang.StringBuilder targetUrl,
java.util.Map<java.lang.String,java.lang.Object> model,
java.lang.String encodingScheme)
Append query properties to the redirect URL.
|
protected java.lang.String |
createTargetUrl(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request)
Creates 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 java.util.Map<java.lang.String,java.lang.String> |
getCurrentRequestUriVariables(HttpServletRequest request) |
protected HttpStatus |
getHttp11StatusCode(HttpServletRequest request,
HttpServletResponse response,
java.lang.String targetUrl)
Determines the status code to use for HTTP 1.1 compatible requests.
|
protected boolean |
isContextRequired()
An ApplicationContext is not strictly required for RedirectView.
|
protected boolean |
isEligibleProperty(java.lang.String key,
java.lang.Object value)
Determine whether the given model element should be exposed
as a query property.
|
protected boolean |
isEligibleValue(java.lang.Object value)
Determine whether the given model element value is eligible for exposure.
|
boolean |
isRedirectView()
Returns "true" indicating this view performs a redirect.
|
protected java.util.Map<java.lang.String,java.lang.Object> |
queryProperties(java.util.Map<java.lang.String,java.lang.Object> model)
Determine name-value pairs for query strings, which will be stringified,
URL-encoded and formatted by
appendQueryProperties(java.lang.StringBuilder, java.util.Map<java.lang.String, java.lang.Object>, java.lang.String) . |
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Convert model to request parameters and redirect to the given URL.
|
protected java.lang.StringBuilder |
replaceUriTemplateVariables(java.lang.String targetUrl,
java.util.Map<java.lang.String,java.lang.Object> model,
java.util.Map<java.lang.String,java.lang.String> currentUriVariables,
java.lang.String encodingScheme)
Replace URI template variables in the target URL with encoded model
attributes or URI variables from the current request.
|
protected void |
sendRedirect(HttpServletRequest request,
HttpServletResponse response,
java.lang.String targetUrl,
boolean http10Compatible)
Send a redirect back to the HTTP client
|
void |
setContextRelative(boolean contextRelative)
Set whether to interpret a given URL that starts with a slash ("/")
as relative to the current ServletContext, i.e.
|
void |
setEncodingScheme(java.lang.String encodingScheme)
Set the encoding scheme for this view.
|
void |
setExpandUriTemplateVariables(boolean expandUriTemplateVariables)
Whether to treat the redirect URL as a URI template.
|
void |
setExposeModelAttributes(boolean exposeModelAttributes)
Set the
exposeModelAttributes flag which denotes whether
or not model attributes should be exposed as HTTP query parameters. |
void |
setHttp10Compatible(boolean http10Compatible)
Set whether to stay compatible with HTTP 1.0 clients.
|
void |
setStatusCode(HttpStatus statusCode)
Set the status code for this view.
|
protected java.lang.String |
updateTargetUrl(java.lang.String targetUrl,
java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Find the registered
RequestDataValueProcessor , if any, and allow
it to update the redirect target URL. |
protected java.lang.String |
urlEncode(java.lang.String input,
java.lang.String encodingScheme)
URL-encode the given input String with the given encoding scheme.
|
afterPropertiesSet, checkResource, getUrl, isUrlRequired, setUrl, toString
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getContentType, render
private static final java.util.regex.Pattern URI_TEMPLATE_VARIABLE_PATTERN
private boolean contextRelative
private boolean http10Compatible
private boolean exposeModelAttributes
private java.lang.String encodingScheme
private HttpStatus statusCode
private boolean expandUriTemplateVariables
public RedirectView()
public RedirectView(java.lang.String url)
The given URL will be considered as relative to the web server, not as relative to the current ServletContext.
url
- the URL to redirect toRedirectView(String, boolean)
public RedirectView(java.lang.String url, boolean contextRelative)
url
- the URL to redirect tocontextRelative
- whether to interpret the given URL as
relative to the current ServletContextpublic RedirectView(java.lang.String url, boolean contextRelative, boolean http10Compatible)
url
- the URL to redirect tocontextRelative
- whether to interpret the given URL as
relative to the current ServletContexthttp10Compatible
- whether to stay compatible with HTTP 1.0 clientspublic RedirectView(java.lang.String url, boolean contextRelative, boolean http10Compatible, boolean exposeModelAttributes)
url
- the URL to redirect tocontextRelative
- whether to interpret the given URL as
relative to the current ServletContexthttp10Compatible
- whether to stay compatible with HTTP 1.0 clientsexposeModelAttributes
- whether or not model attributes should be
exposed as query parameterspublic void setContextRelative(boolean contextRelative)
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.
javax.servlet.http.HttpServletRequest#getContextPath
public void setHttp10Compatible(boolean http10Compatible)
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.
javax.servlet.http.HttpServletResponse#sendRedirect
public void setExposeModelAttributes(boolean exposeModelAttributes)
exposeModelAttributes
flag which denotes whether
or not model attributes should be exposed as HTTP query parameters.
Defaults to true
.
public void setEncodingScheme(java.lang.String encodingScheme)
Default is the request's encoding scheme (which is ISO-8859-1 if not specified otherwise).
public void setStatusCode(HttpStatus statusCode)
Default is to send 302/303, depending on the value of the
http10Compatible
flag.
public void setExpandUriTemplateVariables(boolean expandUriTemplateVariables)
false
if the redirect URL contains open
and close curly braces "{", "}" and you don't want them interpreted
as URI variables.
Defaults to true
.
public boolean isRedirectView()
isRedirectView
in interface SmartView
protected boolean isContextRequired()
isContextRequired
in class WebApplicationObjectSupport
ApplicationObjectSupport.getApplicationContext()
,
ApplicationObjectSupport.getMessageSourceAccessor()
,
WebApplicationObjectSupport.getWebApplicationContext()
,
WebApplicationObjectSupport.getServletContext()
,
WebApplicationObjectSupport.getTempDir()
protected void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.io.IOException
renderMergedOutputModel
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributesrequest
- current HTTP requestresponse
- current HTTP responsejava.io.IOException
appendQueryProperties(java.lang.StringBuilder, java.util.Map<java.lang.String, java.lang.Object>, java.lang.String)
,
sendRedirect(HttpServletRequest, HttpServletResponse, java.lang.String, boolean)
protected final java.lang.String createTargetUrl(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
protected java.lang.StringBuilder replaceUriTemplateVariables(java.lang.String targetUrl, java.util.Map<java.lang.String,java.lang.Object> model, java.util.Map<java.lang.String,java.lang.String> currentUriVariables, java.lang.String encodingScheme) throws java.io.UnsupportedEncodingException
targetUrl
- the redirect URLmodel
- Map that contains model attributescurrentUriVariables
- current request URI variables to useencodingScheme
- the encoding scheme to usejava.io.UnsupportedEncodingException
- if string encoding failedprivate java.util.Map<java.lang.String,java.lang.String> getCurrentRequestUriVariables(HttpServletRequest request)
protected void appendQueryProperties(java.lang.StringBuilder targetUrl, java.util.Map<java.lang.String,java.lang.Object> model, java.lang.String encodingScheme) throws java.io.UnsupportedEncodingException
targetUrl
- the StringBuilder to append the properties tomodel
- Map that contains model attributesencodingScheme
- the encoding scheme to usejava.io.UnsupportedEncodingException
- if string encoding failedqueryProperties(java.util.Map<java.lang.String, java.lang.Object>)
protected java.util.Map<java.lang.String,java.lang.Object> queryProperties(java.util.Map<java.lang.String,java.lang.Object> model)
appendQueryProperties(java.lang.StringBuilder, java.util.Map<java.lang.String, java.lang.Object>, java.lang.String)
.
This implementation filters the model through checking
isEligibleProperty(String, Object)
for each element,
by default accepting Strings, primitives and primitive wrappers only.
model
- the original model MapisEligibleProperty(String, Object)
protected boolean isEligibleProperty(java.lang.String key, java.lang.Object value)
The default implementation considers Strings and primitives as eligible, and also arrays and Collections/Iterables with corresponding elements. This can be overridden in subclasses.
key
- the key of the model elementvalue
- the value of the model elementprotected boolean isEligibleValue(java.lang.Object value)
The default implementation considers primitives, Strings, Numbers, Dates, URIs, URLs and Locale objects as eligible. This can be overridden in subclasses.
value
- the model element valueBeanUtils.isSimpleValueType(java.lang.Class<?>)
protected java.lang.String urlEncode(java.lang.String input, java.lang.String encodingScheme) throws java.io.UnsupportedEncodingException
The default implementation uses URLEncoder.encode(input, enc)
.
input
- the unencoded input StringencodingScheme
- the encoding schemejava.io.UnsupportedEncodingException
- if thrown by the JDK URLEncoderURLEncoder.encode(String, String)
,
URLEncoder.encode(String)
protected java.lang.String updateTargetUrl(java.lang.String targetUrl, java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response)
RequestDataValueProcessor
, if any, and allow
it to update the redirect target URL.protected void sendRedirect(HttpServletRequest request, HttpServletResponse response, java.lang.String targetUrl, boolean http10Compatible) throws java.io.IOException
request
- current HTTP request (allows for reacting to request method)response
- current HTTP response (for sending response headers)targetUrl
- the target URL to redirect tohttp10Compatible
- whether to stay compatible with HTTP 1.0 clientsjava.io.IOException
- if thrown by response methodsprotected HttpStatus getHttp11StatusCode(HttpServletRequest request, HttpServletResponse response, java.lang.String targetUrl)
The default implementation returns the statusCode
property if set, or the value of the View.RESPONSE_STATUS_ATTRIBUTE
attribute.
If neither are set, it defaults to HttpStatus.SEE_OTHER
(303).
request
- the request to inspectresponse
- the servlet responsetargetUrl
- the target URL