2013-06-12 69 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