Skip to content

Commit 57c757a

Browse files
committed
SPR-2692 Update mvc chapter with URI template support in redirect: view names
1 parent d14d826 commit 57c757a

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ public HandlerExceptionResolver handlerExceptionResolver() throws Exception {
302302

303303
private HandlerExceptionResolver createExceptionHandlerExceptionResolver() throws Exception {
304304
ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
305-
resolver.setOrder(0);
306-
305+
307306
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
308307
configurers.configureMessageConverters(converters);
309308
if (converters.size() == 0) {

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters
164164
this.messageConverters = messageConverters;
165165
}
166166

167-
public void afterPropertiesSet() throws Exception {
167+
public void afterPropertiesSet() {
168168
if (argumentResolvers == null) {
169169
argumentResolvers = new HandlerMethodArgumentResolverComposite();
170170
argumentResolvers.addResolvers(customArgumentResolvers);

spring-framework-reference/src/mvc.xml

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,18 +2158,31 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
21582158

21592159
<para>The <classname>RedirectView</classname> issues an
21602160
<literal>HttpServletResponse.sendRedirect()</literal> call that
2161-
returns to the client browser as an HTTP redirect. <!--Does preceding happen after what happens in first paragraph? Clarify sequence of events.-->All
2162-
model attributes are exposed as HTTP query parameters. This means that
2163-
the model must contain only objects (generally Strings or objects
2164-
converted to a String representation), which can be readily converted
2165-
to a textual HTTP query parameter.</para>
2161+
returns to the client browser as an HTTP redirect.
2162+
All model attributes are considered to be exposed as either URI template variables first,
2163+
assuming the URL is a URI template such as <code>/account/{number}</code>,
2164+
or as HTTP query parameters second. By default String and primitive model attributes
2165+
are eligible to be exposed this way. However, this behavior can be extended by
2166+
sub-classing RedirectView. Also consider that <code>@PathVariable</code>-annotated
2167+
method arguments are automatically added to the model, which is convenient when
2168+
redirecting to the same URL using a different HTTP method. For example:</para>
2169+
2170+
<programlisting language="java">@RequestMapping(value = "/files/{path}", method = RequestMethod.POST)
2171+
public String upload(@PathVariable String path, ...) {
2172+
// ...
2173+
return "redirect:files/{path};
2174+
}
2175+
2176+
@RequestMapping(value = "/files/{path}", method = RequestMethod.GET)
2177+
public void get(@PathVariable String path, ...) {
2178+
// ...
2179+
}</programlisting>
21662180

21672181
<para>If you use <classname>RedirectView</classname> and the view is
21682182
created by the controller itself, it is recommended that you configure
21692183
the redirect URL to be injected into the controller so that it is not
21702184
baked into the controller but configured in the context along with the
2171-
view names. <!--I revised sentence because it sounds like something you need to do. Also reworded next heading to say what it's about. If not correct,--><!--reword.-->The
2172-
next section discusses this process.</para>
2185+
view names. The next section discusses this process.</para>
21732186
</section>
21742187

21752188
<section id="mvc-redirecting-redirect-prefix">
@@ -2194,13 +2207,10 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
21942207
<para>The net effect is the same as if the controller had returned a
21952208
<classname>RedirectView</classname>, but now the controller itself can
21962209
simply operate in terms of logical view names. A logical view name
2197-
such as <literal>redirect:/my/response/controller.html</literal> will
2210+
such as <literal>redirect:/myapp/some/resource</literal> will
21982211
redirect relative to the current servlet context, while a name such as
2199-
<literal>redirect:http://myhost.com/some/arbitrary/path.html</literal>
2200-
will redirect to an absolute URL. The important thing is that, as long
2201-
as this redirect view name is injected into the controller like any
2202-
other logical view name, the controller is not even aware that
2203-
redirection is happening.</para>
2212+
<literal>redirect:http://myhost.com/some/arbitrary/path</literal>
2213+
will redirect to an absolute URL.</para>
22042214
</section>
22052215

22062216
<section id="mvc-redirecting-forward-prefix">

0 commit comments

Comments
 (0)