2012-07-05 69 views
5

我想用RedirectAttibutes屬性,它已經出現在春季3.1,我在控制器後以下處理方法RedirectAttributes在春季給IllegalStateException異常3.1

@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"; 
} 

但是,當我提交表單我「M收到以下異常:

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) 

我所遇到的一些疑難雜症的與您不能使用的ModelAndView作爲返回類型redirectAttributes。所以我只返回了字符串視圖。

任何人都可以。告訴我我要去哪裏?

謝謝。

回答

15

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>)。

+1

添加解決了問題!!!謝謝。 – tintin 2012-07-05 16:09:45

+1

控制器的@EnableWebMvc解決了這個問題。應該在哪裏添加「mvc:annotation-driven」? – 2013-06-03 22:06:36