【问题标题】:RedirectAttributes giving IllegalStateException in Spring 3.1RedirectAttributes 在 Spring 3.1 中给出 IllegalStateException
【发布时间】:2012-07-05 15:15:33
【问题描述】:

我想使用 Spring 3.1 中出现的 RedirectAttibutes 属性,我的控制器中有以下处理程序方法用于发布

    @RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@ModelAttribute("admin") Admin admin, BindingResult bindingResult, SessionStatus sessionStatus, RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("admin", admin);
    if (bindingResult.hasErrors()) {
        return REGISTRATION_VIEW;

    }
    sessionStatus.setComplete();
    return "redirect:list";
}

但是当我提交表单时出现以下异常:

java.lang.IllegalStateException: Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure classes to use this argument.
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:322)

我遇到了一些关于 redirectAttributes 的问题,你不能使用 ModelAndView 作为返回类型。所以我只返回了字符串视图。

任何人都可以请。告诉我哪里出错了?

谢谢。

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    Spring 3.1 引入了新版本的 Spring MVC 后端实现 (RequestMappingHandlerMapping/RequestMappingHandlerAdapter) 以取代旧版本 (DefaultAnnotationHandlerMapping/AnnotationMethodHandlerAdapter)。

    Spring MVC 3.1 的一些新特性,例如RedirectAttributes,仅受新实现支持。

    如果您使用<mvc:annotation-driven>@EnableWebMvc 启用Spring MVC,则应默认启用新实现。但是,如果您手动声明 HandlerMapping 和/或 HandlerAdapter 或使用默认值,则需要显式切换到新实现(例如,通过切换到 <mvc:annotation-driven>,如果它不会破坏您的配置) .

    【讨论】:

    • @EnableWebMvc 为控制器解决了这个问题。应该在哪里添加“mvc:annotation-driven”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    相关资源
    最近更新 更多