Improve RedirectAttributes reference docs
The use of RedirectAttributes was documented in four places in the reference documentation. This commit merges some of the places and links properly between them. Issue: SPR-12759
This commit is contained in:
parent
b119a9c82c
commit
463878a03f
|
@ -1169,8 +1169,8 @@ multiple requests are allowed to access a session concurrently.
|
||||||
* `org.springframework.web.servlet.mvc.support.RedirectAttributes` to specify the exact
|
* `org.springframework.web.servlet.mvc.support.RedirectAttributes` to specify the exact
|
||||||
set of attributes to use in case of a redirect and also to add flash attributes
|
set of attributes to use in case of a redirect and also to add flash attributes
|
||||||
(attributes stored temporarily on the server-side to make them available to the
|
(attributes stored temporarily on the server-side to make them available to the
|
||||||
request after the redirect). `RedirectAttributes` is used instead of the implicit
|
request after the redirect). See <<mvc-redirecting-passing-data>> and
|
||||||
model if the method returns a "redirect:" prefixed view name or `RedirectView`.
|
<<mvc-flash-attributes>>.
|
||||||
* Command or form objects to bind request parameters to bean properties (via setters)
|
* Command or form objects to bind request parameters to bean properties (via setters)
|
||||||
or directly to fields, with customizable type conversion, depending on `@InitBinder`
|
or directly to fields, with customizable type conversion, depending on `@InitBinder`
|
||||||
methods and/or the HandlerAdapter configuration. See the `webBindingInitializer`
|
methods and/or the HandlerAdapter configuration. See the `webBindingInitializer`
|
||||||
|
@ -1674,34 +1674,6 @@ attribute name:
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
[[mvc-ann-redirect-attributes]]
|
|
||||||
==== Specifying redirect and flash attributes
|
|
||||||
By default all model attributes are considered to be exposed as URI template variables
|
|
||||||
in the redirect URL. Of the remaining attributes those that are primitive types or
|
|
||||||
collections/arrays of primitive types are automatically appended as query parameters.
|
|
||||||
|
|
||||||
In annotated controllers however the model may contain additional attributes originally
|
|
||||||
added for rendering purposes (e.g. drop-down field values). To gain precise control over
|
|
||||||
the attributes used in a redirect scenario, an `@RequestMapping` method can declare an
|
|
||||||
argument of type `RedirectAttributes` and use it to add attributes for use in
|
|
||||||
`RedirectView`. If the controller method does redirect, the content of
|
|
||||||
`RedirectAttributes` is used. Otherwise the content of the default `Model` is used.
|
|
||||||
|
|
||||||
The `RequestMappingHandlerAdapter` provides a flag called
|
|
||||||
`"ignoreDefaultModelOnRedirect"` that can be used to indicate the content of the default
|
|
||||||
`Model` should never be used if a controller method redirects. Instead the controller
|
|
||||||
method should declare an attribute of type `RedirectAttributes` or if it doesn't do so
|
|
||||||
no attributes should be passed on to `RedirectView`. Both the MVC namespace and the MVC
|
|
||||||
Java config keep this flag set to `false` in order to maintain backwards compatibility.
|
|
||||||
However, for new applications we recommend setting it to `true`
|
|
||||||
|
|
||||||
The `RedirectAttributes` interface can also be used to add flash attributes. Unlike
|
|
||||||
other redirect attributes, which end up in the target redirect URL, flash attributes are
|
|
||||||
saved in the HTTP session (and hence do not appear in the URL). The model of the
|
|
||||||
controller serving the target redirect URL automatically receives these flash attributes
|
|
||||||
after which they are removed from the session. See <<mvc-flash-attributes>> for an
|
|
||||||
overview of the general support for flash attributes in Spring MVC.
|
|
||||||
|
|
||||||
|
|
||||||
[[mvc-ann-form-urlencoded-data]]
|
[[mvc-ann-form-urlencoded-data]]
|
||||||
==== Working with "application/x-www-form-urlencoded" data
|
==== Working with "application/x-www-form-urlencoded" data
|
||||||
|
@ -2694,7 +2666,7 @@ the last results in the chain not being fully inspected, because the
|
||||||
|
|
||||||
|
|
||||||
[[mvc-redirecting]]
|
[[mvc-redirecting]]
|
||||||
=== Redirecting to views
|
=== Redirecting to Views
|
||||||
As mentioned previously, a controller typically returns a logical view name, which a
|
As mentioned previously, a controller typically returns a logical view name, which a
|
||||||
view resolver resolves to a particular view technology. For view technologies such as
|
view resolver resolves to a particular view technology. For view technologies such as
|
||||||
JSPs that are processed through the Servlet or JSP engine, this resolution is usually
|
JSPs that are processed through the Servlet or JSP engine, this resolution is usually
|
||||||
|
@ -2727,22 +2699,37 @@ One way to force a redirect as the result of a controller response is for the co
|
||||||
to create and return an instance of Spring's `RedirectView`. In this case,
|
to create and return an instance of Spring's `RedirectView`. In this case,
|
||||||
`DispatcherServlet` does not use the normal view resolution mechanism. Rather because it
|
`DispatcherServlet` does not use the normal view resolution mechanism. Rather because it
|
||||||
has been given the (redirect) view already, the `DispatcherServlet` simply instructs the
|
has been given the (redirect) view already, the `DispatcherServlet` simply instructs the
|
||||||
view to do its work.
|
view to do its work. The `RedirectView` in turn calls `HttpServletResponse.sendRedirect()`
|
||||||
|
to send an HTTP redirect to the client browser.
|
||||||
|
|
||||||
The `RedirectView` issues an `HttpServletResponse.sendRedirect()` call that returns to
|
If you use `RedirectView` and the view is created by the controller itself, it is
|
||||||
the client browser as an HTTP redirect. By default all model attributes are considered
|
recommended that you configure the redirect URL to be injected into the controller so
|
||||||
to be exposed as URI template variables in the redirect URL. Of the remaining attributes
|
that it is not baked into the controller but configured in the context along with the
|
||||||
those that are primitive types or collections/arrays of primitive types are
|
view names. The <<mvc-redirecting-redirect-prefix>> facilitates this decoupling.
|
||||||
automatically appended as query parameters.
|
|
||||||
|
[[mvc-redirecting-passing-data]]
|
||||||
|
===== Passing Data To the Redirect Target
|
||||||
|
|
||||||
|
By default all model attributes are considered to be exposed as URI template variables in
|
||||||
|
the redirect URL. Of the remaining attributes those that are primitive types or
|
||||||
|
collections/arrays of primitive types are automatically appended as query parameters.
|
||||||
|
|
||||||
Appending primitive type attributes as query parameters may be the desired result if a
|
Appending primitive type attributes as query parameters may be the desired result if a
|
||||||
model instance was prepared specifically for the redirect. However, in annotated
|
model instance was prepared specifically for the redirect. However, in annotated
|
||||||
controllers the model may contain additional attributes added for rendering purposes
|
controllers the model may contain additional attributes added for rendering purposes (e.g.
|
||||||
(e.g. drop-down field values). To avoid the possibility of having such attributes appear
|
drop-down field values). To avoid the possibility of having such attributes appear in the
|
||||||
in the URL an annotated controller can declare an argument of type `RedirectAttributes`
|
URL, an `@RequestMapping` method can declare an argument of type `RedirectAttributes` and
|
||||||
and use it to specify the exact attributes to make available to `RedirectView`. If the
|
use it to specify the exact attributes to make available to `RedirectView`. If the method
|
||||||
controller method decides to redirect, the content of `RedirectAttributes` is used.
|
does redirect, the content of `RedirectAttributes` is used. Otherwise the content of the
|
||||||
Otherwise the content of the model is used.
|
model is used.
|
||||||
|
|
||||||
|
The `RequestMappingHandlerAdapter` provides a flag called
|
||||||
|
`"ignoreDefaultModelOnRedirect"` that can be used to indicate the content of the default
|
||||||
|
`Model` should never be used if a controller method redirects. Instead the controller
|
||||||
|
method should declare an attribute of type `RedirectAttributes` or if it doesn't do so
|
||||||
|
no attributes should be passed on to `RedirectView`. Both the MVC namespace and the MVC
|
||||||
|
Java config keep this flag set to `false` in order to maintain backwards compatibility.
|
||||||
|
However, for new applications we recommend setting it to `true`
|
||||||
|
|
||||||
Note that URI template variables from the present request are automatically made
|
Note that URI template variables from the present request are automatically made
|
||||||
available when expanding a redirect URL and do not need to be added explicitly neither
|
available when expanding a redirect URL and do not need to be added explicitly neither
|
||||||
|
@ -2758,11 +2745,9 @@ through `Model` nor `RedirectAttributes`. For example:
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
If you use `RedirectView` and the view is created by the controller itself, it is
|
Another way of passing data to the redirect target is via __Flash Attributes__. Unlike
|
||||||
recommended that you configure the redirect URL to be injected into the controller so
|
other redirect attributes, flash attributes are saved in the HTTP session (and hence do
|
||||||
that it is not baked into the controller but configured in the context along with the
|
not appear in the URL). See <<mvc-flash-attributes>> for more information.
|
||||||
view names. The next section discusses this process.
|
|
||||||
|
|
||||||
|
|
||||||
[[mvc-redirecting-redirect-prefix]]
|
[[mvc-redirecting-redirect-prefix]]
|
||||||
==== The redirect: prefix
|
==== The redirect: prefix
|
||||||
|
@ -2967,8 +2952,8 @@ are accessible from anywhere in Spring MVC through static methods in
|
||||||
Annotated controllers typically do not need to work with `FlashMap` directly. Instead an
|
Annotated controllers typically do not need to work with `FlashMap` directly. Instead an
|
||||||
`@RequestMapping` method can accept an argument of type `RedirectAttributes` and use it
|
`@RequestMapping` method can accept an argument of type `RedirectAttributes` and use it
|
||||||
to add flash attributes for a redirect scenario. Flash attributes added via
|
to add flash attributes for a redirect scenario. Flash attributes added via
|
||||||
`RedirectAttributes` are automatically propagated to the "output" FlashMap. Similarly
|
`RedirectAttributes` are automatically propagated to the "output" FlashMap. Similarly,
|
||||||
after the redirect attributes from the "input" `FlashMap` are automatically added to the
|
after the redirect, attributes from the "input" `FlashMap` are automatically added to the
|
||||||
`Model` of the controller serving the target URL.
|
`Model` of the controller serving the target URL.
|
||||||
|
|
||||||
.Matching requests to flash attributes
|
.Matching requests to flash attributes
|
||||||
|
|
Loading…
Reference in New Issue