springmodel-view-controllerhttp-redirectflash-scope

Spring MVC FlashMap and RedirectAttributes request mapping


Yesterday I downloaded the new Spring 3.1RC to test the just introduced support for flash scoped variables in Spring MVC. Unfortunately I could not make it working...

I have a HTML form containing some checkboxes without spring:forms tags. Something like this:

<form action="/deleteaction" method="post">
<input type="checkbox" name="itemId" value="1" />
<input type="checkbox" name="itemId" value="2" />
<input type="submit" name="delete" value="Delete items" />
</form>

Before the Flash scope support, my annotated controller looked like:

@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[]) {

Alternatively, I could have used an HttpServletRequest instead of @RequestParam:

@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
public String deleteItems(HttpServletRequest request) {
    String itemIds[] = request.getParameterValues("itemId");

Both methods were working fine. If I try to add the RedirectAttributes to the method parameters, Spring will throw an exception:

@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[], RedirectAttributes redirectAttrs) {

Log:

Oct 16, 2011 11:20:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/App] threw
    exception [Request processing failed; nested exception is
    java.lang.IllegalArgumentException: argument type mismatch] with root cause 
    java.lang.IllegalArgumentException: argument type mismatch

What am I doing wrong? How is it possible to get the RedirectAttribues parameter where to add flash scoped variables?


Solution

  • In order to use the new RedirectAttributes, the old DefaultAnnotationHandlerMapping, AnnotationMethodHandlerAdapter and AnnotationMethodHandlerExceptionResolver must be replaced in the dispatcher-servlet.xml.

    One chance is to use <mvc:annotation-driven/>, which in Spring 3.1 configures new classes RequestMappingHandlerMapping, RequestMappingHandlerAdapter, and ExceptionHandlerExceptionResolver replacing the old ones.

    Otherwise, the new handler classes must be explicitly configured in dispatcher-servlet.xml.

    Some extra info here: http://forum.springsource.org/showthread.php?115976-Spring-MVC-FlashMap-and-RedirectAttributes-request-mapping