2010-05-13 56 views
3

我試圖建立這樣一個控制器:Spring MVC的默認值的雙

@RequestMapping(method = {RequestMethod.GET}, value = "https://stackoverflow.com/users/detail/activities.do") 
public View foo(@RequestParam(value = "userCash", defaultValue="0.0") Double userCash) 
{ 
    System.out.println("foo userCash=" + userCash); 
} 

這工作得很好:

http://localhost/app/users/detail/activities.do?userCash=123&

但在這一個userCash == NULL儘管默認值

http://localhost/app/users/detail/activities.do?userCash=&

從一些挖吉寧它似乎是第一個工程B/C一個編輯器是這樣結合的:

binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, false)); 

麻煩的是,所述第二參數(即假)定義空白值是否是允許的。如果我將其設置爲true,那麼系統將空白輸入視爲有效,所以我得到一個空Double類。

如果我將它設置爲false,那麼系統扼流圈與空白輸入字符串:

org.springframework.beans.TypeMismatchException: 未能轉換 類型的值「java.lang.String中」到需要的類型 'double';嵌套的例外是 java.lang.NumberFormatException:空字符串

有誰知道如何得到默認值,爲雙打工作?

+0

這聽起來像一個錯誤,或至少是一個合理的要求,以提高我。你有沒有考慮在JIRA開一張票? http://jira.springframework.org – 2010-05-13 07:02:03

回答

0

可能你必須使用自己的CustomEditor來實現你的轉換邏輯。

從請求的角度來看,像/activities.do?userCash= &這樣的URL意味着userCash參數實際上是一個空白或空字符串(因此您看到的錯誤)。