4

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

我有一个 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[]) {

或者,我可以使用 anHttpServletRequest而不是@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 参数来添加 Flash 范围变量?

4

1 回答 1

4

为了使用新RedirectAttributes的,旧的,DefaultAnnotationHandlerMapping必须更换的。AnnotationMethodHandlerAdapterAnnotationMethodHandlerExceptionResolverdispatcher-servlet.xml

一个机会是使用<mvc:annotation-driven/>,它在 Spring 3.1 中配置新的类RequestMappingHandlerMapping,RequestMappingHandlerAdapterExceptionHandlerExceptionResolver替换旧的。

否则,新的处理程序类必须在dispatcher-servlet.xml.

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

于 2011-10-18T06:59:55.893 回答