Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectAttributes giving IllegalStateException in Spring 3.1

I'm want to use RedirectAttibutes property that has come up in Spring 3.1, I have the following handler method for post in my 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";
}

But when I submit the form I'm getting the following exception:

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)

I have come across a few gotcha's with redirectAttributes that you cannot use ModelAndView as the return type. So I returned just the string view.

Can anyone pl. tell me where I'm going wrong?

Thanks.

like image 298
tintin Avatar asked Jul 05 '12 15:07

tintin


1 Answers

Spring 3.1 introduced new version of Spring MVC backend implementation (RequestMappingHandlerMapping/RequestMappingHandlerAdapter) to replace the old one (DefaultAnnotationHandlerMapping/AnnotationMethodHandlerAdapter).

Some new features of Spring MVC 3.1, such as RedirectAttributes, are only supported by the new implementation.

If you use <mvc:annotation-driven> or @EnableWebMvc to enable Spring MVC, new implementation should be enabled by default. However, if you declare HandlerMapping and/or HandlerAdapter manually or use the default ones, you need to switch to the new implementation explicitly (for example, by switching to <mvc:annotation-driven>, if it doesn't break your configuration).

like image 53
axtavt Avatar answered Sep 28 '22 06:09

axtavt