|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.AbstractUrlBasedView
org.springframework.web.servlet.view.RedirectView
public class RedirectView
View that redirects to an absolute, context relative, or current request relative URL, by default exposing all model attributes as HTTP query parameters.
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.
When e.g. using SimpleFormController
,
make sure to set your controller's
"redirectAction"
property to "true", in order to make the controller base class behave accordingly.
setContextRelative(boolean)
,
setHttp10Compatible(boolean)
,
setExposeModelAttributes(boolean)
,
HttpServletResponse.sendRedirect(java.lang.String)
Field Summary |
---|
Fields inherited from class org.springframework.web.servlet.view.AbstractView |
---|
DEFAULT_CONTENT_TYPE |
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport |
---|
logger |
Constructor Summary | |
---|---|
RedirectView()
Constructor for use as a bean. |
|
RedirectView(String url)
Create a new RedirectView with the given URL. |
|
RedirectView(String url,
boolean contextRelative)
Create a new RedirectView with the given URL. |
|
RedirectView(String url,
boolean contextRelative,
boolean http10Compatible)
Create a new RedirectView with the given URL. |
|
RedirectView(String url,
boolean contextRelative,
boolean http10Compatible,
boolean exposeModelAttributes)
Create a new RedirectView with the given URL. |
Method Summary | |
---|---|
protected void |
appendQueryProperties(StringBuffer targetUrl,
Map model,
String encodingScheme)
Append query properties to the redirect URL. |
protected boolean |
isEligibleProperty(String key,
Object value)
Determine whether the given model element should be exposed as a query property. |
protected boolean |
isEligibleValue(Object value)
Determine whether the given model element value is eligible for exposure. |
protected Map |
queryProperties(Map model)
Determine name-value pairs for query strings, which will be stringified, URL-encoded and formatted by appendQueryProperties(java.lang.StringBuffer, java.util.Map, java.lang.String) . |
protected void |
renderMergedOutputModel(Map model,
HttpServletRequest request,
HttpServletResponse response)
Convert model to request parameters and redirect to the given URL. |
protected void |
sendRedirect(HttpServletRequest request,
HttpServletResponse response,
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. as relative to the web application root. |
void |
setEncodingScheme(String encodingScheme)
Set the encoding scheme for this view. |
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. |
protected String |
urlEncode(String input,
String encodingScheme)
URL-encode the given input String with the given encoding scheme. |
Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView |
---|
afterPropertiesSet, getUrl, isUrlRequired, setUrl, toString |
Methods inherited from class org.springframework.web.servlet.view.AbstractView |
---|
addStaticAttribute, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute, writeToResponse |
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport |
---|
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext |
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport |
---|
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public RedirectView()
public RedirectView(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(String url, boolean contextRelative)
url
- the URL to redirect tocontextRelative
- whether to interpret the given URL as
relative to the current ServletContextpublic RedirectView(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(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 parametersMethod Detail |
---|
public 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.
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.
HttpServletResponse.sendRedirect(java.lang.String)
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(String encodingScheme)
Default is the request's encoding scheme (which is ISO-8859-1 if not specified otherwise).
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws IOException
renderMergedOutputModel
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributesrequest
- current HTTP requestresponse
- current HTTP response
IOException
appendQueryProperties(java.lang.StringBuffer, java.util.Map, java.lang.String)
,
sendRedirect(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, boolean)
protected void appendQueryProperties(StringBuffer targetUrl, Map model, String encodingScheme) throws UnsupportedEncodingException
targetUrl
- the StringBuffer to append the properties tomodel
- Map that contains model attributesencodingScheme
- the encoding scheme to use
UnsupportedEncodingException
- if string encoding failedqueryProperties(java.util.Map)
protected Map queryProperties(Map model)
appendQueryProperties(java.lang.StringBuffer, java.util.Map, 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 Map
isEligibleProperty(String, Object)
protected boolean isEligibleProperty(String key, 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 element
protected boolean isEligibleValue(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 value
BeanUtils.isSimpleValueType(java.lang.Class)
protected String urlEncode(String input, String encodingScheme) throws UnsupportedEncodingException
The default implementation uses URLEncoder.encode(input, enc)
.
input
- the unencoded input StringencodingScheme
- the encoding scheme
UnsupportedEncodingException
- if thrown by the JDK URLEncoderURLEncoder.encode(String, String)
,
URLEncoder.encode(String)
protected void sendRedirect(HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean http10Compatible) throws 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 clients
IOException
- if thrown by response methods
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |