Cia*_*cia 4 model-view-controller redirect spring flash-scope
昨天我下载了新的 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>
Run Code Online (Sandbox Code Playgroud)
在 Flash 范围支持之前,我的带注释的控制器如下所示:
@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[]) {
Run Code Online (Sandbox Code Playgroud)
或者,我可以使用 anHttpServletRequest代替@RequestParam:
@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
public String deleteItems(HttpServletRequest request) {
String itemIds[] = request.getParameterValues("itemId");
Run Code Online (Sandbox Code Playgroud)
两种方法都运行良好。如果我尝试将 添加到RedirectAttributes方法参数中,Spring 将抛出异常:
@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[], RedirectAttributes redirectAttrs) {
Run Code Online (Sandbox Code Playgroud)
日志:
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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如何获取 RedirectAttribues 参数来添加 flash 作用域变量?
为了使用新的RedirectAttributes,旧的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter必须AnnotationMethodHandlerExceptionResolver更换dispatcher-servlet.xml。
一个机会是使用<mvc:annotation-driven/>,它在 Spring 3.1 中配置新类RequestMappingHandlerMapping、RequestMappingHandlerAdapter,并ExceptionHandlerExceptionResolver替换旧类。
否则,新的处理程序类必须在dispatcher-servlet.xml.
| 归档时间: |
|
| 查看次数: |
6210 次 |
| 最近记录: |