Spring MVC FlashMap和RedirectAttributes请求映射

时间:2011-10-16 21:27:43

标签: spring model-view-controller redirect flash-scope

昨天我下载了新的Spring 3.1RC来测试Spring MVC中刚刚引入的flash scoped变量支持。不幸的是我无法让它工作......

我有一个HTML表单,其中包含一些没有spring:forms标签的复选框。像这样:

<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>

在Flash范围支持之前,我的带注释的控制器看起来像:

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

或者,我可以使用HttpServletRequest代替@RequestParam

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

这两种方法都运行良好。如果我尝试将RedirectAttributes添加到方法参数中,Spring将抛出异常:

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

日志:

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

我做错了什么?如何获取RedirectAttribues参数在哪里添加闪存范围的变量?

1 个答案:

答案 0 :(得分:4)

要使用新的RedirectAttributes,必须在DefaultAnnotationHandlerMapping中替换旧的AnnotationMethodHandlerAdapterAnnotationMethodHandlerExceptionResolverdispatcher-servlet.xml

有一种机会是使用<mvc:annotation-driven/>,它在Spring 3.1中配置新类RequestMappingHandlerMappingRequestMappingHandlerAdapterExceptionHandlerExceptionResolver来替换旧类。

否则,必须在dispatcher-servlet.xml中显式配置新的处理程序类。

这里有一些额外的信息:http://forum.springsource.org/showthread.php?115976-Spring-MVC-FlashMap-and-RedirectAttributes-request-mapping