昨天我下载了新的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[]) {
或者,我可以使用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
我做错了什么?如何获得redirectattributes参数在哪里添加flash作用域变量?
为了使用新的RedirectAttributes
,必须在dispatcher-servlet.xml
中替换旧的DefaultAnnotationHandlerMapping
, AnnotationMethodHandlerAdapter
和AnnotationMethodHandlerExceptionResolver
。
一个机会是使用<mvc:annotation-driven/>
,它在Spring 3.1中配置新的类RequestMappingHandlerMapping
, RequestMappingHandlerAdapter
和ExceptionHandlerExceptionResolver
来取代旧的。
否则,必须在dispatcher-servlet.xml
中显式配置新的处理程序类。
这里有一些额外的信息:http://forum.springsource.org/showthread.php?115976-Spring-MVC-FlashMap-and-RedirectAttributes-request-mapping