2011-10-16 365 views
3

昨天我下載了新的Spring 3.1RC來測試Spring MVC中對flash範圍變量的剛剛引入的支持。不幸的是,我無法使它工作...我有一個HTML表單,其中包含一些沒有彈簧的複選框:窗體標籤。事情是這樣的:Spring MVC FlashMap和RedirectAttributes請求映射

<form action="/deleteaction" method="post"> 
<input type="checkbox" name="itemId" value="1" /> 
<input type="checkbox" name="itemId" value="2" /> 
<input type="submit" name="delete" value="Delete items" /> 
</form> 

閃光燈範圍支持之前,我的註解控制器看起來像:

@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"}) 
public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[]) { 

或者,我可以使用的HttpServletRequest代替@RequestParam

@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"}) 
public String deleteItems(HttpServletRequest request) { 
    String itemIds[] = request.getParameterValues("itemId"); 

兩個方法工作正常。如果我嘗試添加RedirectAttributes到方法的參數,Spring會拋出異常:

@RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"}) 
public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[], RedirectAttributes redirectAttrs) { 

登錄:

Oct 16, 2011 11:20:37 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/App] threw 
    exception [Request processing failed; nested exception is 
    java.lang.IllegalArgumentException: argument type mismatch] with root cause 
    java.lang.IllegalArgumentException: argument type mismatch 

我在做什麼錯?如何獲得RedirectAttribues參數添加Flash範圍變量的地方?

回答

4

爲了使用新RedirectAttributes,老DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapterAnnotationMethodHandlerExceptionResolver必須在dispatcher-servlet.xml取代。

一個機會是使用<mvc:annotation-driven/>,這在Spring 3.1配置新的類RequestMappingHandlerMappingRequestMappingHandlerAdapterExceptionHandlerExceptionResolver更換舊的。

否則,必須在dispatcher-servlet.xml中明確配置新的處理程序類。

一些額外的信息在這裏:http://forum.springsource.org/showthread.php?115976-Spring-MVC-FlashMap-and-RedirectAttributes-request-mapping