to specify the exact 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 request after the redirect).
<literal>RedirectAttributes</literal> is used instead of the implicit
model if the method returns a "redirect:" prefixed view name or
<classname>RedirectView</classname>.
</para>
</listitem>
<listitem>
<para>Command or form objects to bind request parameters to bean
properties (via setters) or directly to fields, with
@ -1697,9 +1709,56 @@ public class EditPetForm {
@@ -1697,9 +1709,56 @@ public class EditPetForm {
</para>
</note>
</section>
<sectionid="mvc-ann-redirect-attributes">
<title>Specifying redirect and flash attributes</title>
<para>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.
</para>
<para>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 <interfacename>@RequestMapping</interfacename> method can declare an
argument of type <interfacename>RedirectAttributes</interfacename>
and use it to add attributes for use in <classname>RedirectView</classname>.
If the controller method does redirect, the content of
<interfacename>RedirectAttributes</interfacename> is used.
Otherwise the content of the default <interfacename>Model</interfacename>
is used.
</para>
<para>
The <classname>RequestMappingHandlerAdapter</classname> provides a flag
called <literal>"ignoreDefaultModelOnRedirect"</literal> that can be
used to indicate the content of the default <interfacename>Model</interfacename>
should never be used if a controller method redirects.
Instead the controller method should declare an attribute of type
<interfacename>RedirectAttributes</interfacename> or if it doesn't do so
no attributes should be passed on to <classname>RedirectView</classname>.
Both the MVC namespace and the MVC Java config (via
<interfacename>@EnableWebMvc</interfacename>) automatically set
this flag to <literal>true</literal>.
</para>
<para>The <interfacename>RedirectAttributes</interfacename> 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 <xreflinkend="mvc-flash-attributes"/> for an overview of the general
support for flash attributes in Spring MVC.
</para>
</section>
<sectionid="mvc-ann-form-urlencoded-data">
<title>Working with <literal>application/x-www-form-urlencoded</literal> data</title>
<title>Working with <literal>"application/x-www-form-urlencoded"</literal> data</title>
<para>The previous sections covered use of <interfacename>@ModelAttribute</interfacename>
to support form submission requests from browser clients. The same annotation is
@ -2431,24 +2490,38 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
@@ -2431,24 +2490,38 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
<para>The <classname>RedirectView</classname> issues an
<literal>HttpServletResponse.sendRedirect()</literal> call that
returns to the client browser as an HTTP redirect.
All model attributes are considered to be exposed as either URI template variables first,
assuming the URL is a URI template such as <code>/account/{number}</code>,
or as HTTP query parameters second. By default String and primitive model attributes
are eligible to be exposed this way. However, this behavior can be extended by
sub-classing RedirectView. Also consider that <code>@PathVariable</code>-annotated
method arguments are automatically added to the model, which is convenient when
redirecting to the same URL using a different HTTP method. For example:</para>
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.
</para>
<para>
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 controllers the model may contain
additional attributes added for rendering purposes (e.g. drop-down
field values). To avoid the possibility of having such attributes
appear in the URL an annotated controller can declare an
argument of type <interfacename>RedirectAttributes</interfacename>
and use it to specify the exact attributes to make available to
<classname>RedirectView</classname>. If the controller method decides
to redirect, the content of <interfacename>RedirectAttributes</interfacename>
is used. Otherwise the content of the model is used.
</para>
<para>
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 through <interfacename>Model</interfacename> nor
<interfacename>RedirectAttributes</interfacename>. For example:</para>