RedirectAttributes在Spring 3.1中给出IllegalStateException

tin*_*tin 5 spring spring-mvc

我想使用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";
}
Run Code Online (Sandbox Code Playgroud)

但是当我提交表单时,我得到以下异常:

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)
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

axt*_*avt 15

Spring 3.1引入了新版本的Spring MVC后端实现(RequestMappingHandlerMapping/ RequestMappingHandlerAdapter)来替换旧的(DefaultAnnotationHandlerMapping/ AnnotationMethodHandlerAdapter).

Spring MVC 3.1的一些新功能,例如RedirectAttributes,只有新实现支持.

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