spring - RedirectAttributes 在 Spring 3.1 中给出 IllegalStateException

标签 spring spring-mvc

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

    @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 作为返回类型。所以我只返回了字符串 View 。

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

谢谢。

最佳答案

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> ,如果它不会破坏您的配置)。

关于spring - RedirectAttributes 在 Spring 3.1 中给出 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347337/

相关文章:

spring-mvc - Spring Boot + 云 | Zuul 代理 | 404错误

java - 如何在 Spring Boot 中针对特定条件或用户角色@JsonIgnore

java - 为什么 PetClinic 示例中 Vets 类的 VetList 方法使用 XMLElement?

java - Spring 安全: how to map Active Directory users to application users?

java - 如何在 spring 中配置基于语言环境的日期格式支持

xml - 如何在xml中使路径位置动态化

spring - log4j 配置不显示消息 spring mvc

spring - 在 Spring Boot/Framework 中创建一个新的组件实例

java - Spring MVC 绑定(bind)重写表单输入上的 value 属性

java - JSP中刷新进度条