2013-06-12 70 views
1

我正在使用Spring MVC 2.5。覆蓋BindingResult中的錯誤消息

我有字段,其中數字應該只允許放入。我在我的用戶界面上找到確切的錯誤信息。類似於

Failed to convert property value of type [java.lang.String] to required type [java.math.BigDecimal] for property executionThresholdAmount; nested exception is java.lang.NumberFormatException 

我不想向用戶顯示那種類型的消息。我確實使用message.properties文件來組織要顯示的文本。

我唯一需要的是我想覆蓋特定字段的錯誤信息。我不能這樣做,但這裏的伎倆,我使用

if(result.hasFieldErrors()){ 

      List<FieldError> bindingErrors=(List<FieldError>)result.getFieldErrors(); 
      BindingResult fieldErrors=new BeanPropertyBindingResult (offerSetting, "offerSetting"); 
      for(FieldError error :bindingErrors){ 
       String field=error.getField();     
       fieldErrors.rejectValue(field, "invalid.amount."+field); 


      } 


      result=fieldErrors; 
      #more code 

我在做什麼是我創建BeanPropertyBindingResult這是BindingResult的實現和填充我要留言錯誤領域,並通過參考將結果對象顯示出來。但是,我現在同時收到默認消息

like 

Failed to convert property value of type [java.lang.String] to required type [java.math.BigDecimal] for property executionThresholdAmount; nested exception is java.lang.NumberFormatException 

以及我想要的消息。類似於

"The amount for field price you entered is invalid" 

有什麼更好的點子嗎?

+0

以前從未做到了,但也許你可以在你的綁定類中添加try catch? –

+0

@DanielRobertus請看下面的選擇答案 – WowBow

回答

8

嘗試添加到您的郵件屬性文件somethig這樣的:

typeMismatch.objectClassName.field=Your custom message

你的情況:

typeMismatch.offerSetting.amount=The amount for field price you entered is invalid

爲我工作:)

+1

Sweeeet !!!你在早上讓我開心。我無法想象我的一天會變成什麼樣子。 :D – WowBow

+0

哈哈,我很高興工作! ;) – jelies

+1

非常好,謝謝你。 – Manglesh