在BindingResult中覆盖错误消息

Wow*_*Bow 2 java data-binding error-handling spring-mvc

我正在使用Spring MVC 2.5.

我有字段,数字应该只是put中允许的.我在我正在寻找的UI上收到确切的错误消息.就像是

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
Run Code Online (Sandbox Code Playgroud)

我不想向用户显示这种消息.我使用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
Run Code Online (Sandbox Code Playgroud)

我正在做的是我创建了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
Run Code Online (Sandbox Code Playgroud)

还有我想要的信息.就像是

"The amount for field price you entered is invalid"
Run Code Online (Sandbox Code Playgroud)

有更好的想法吗?

jel*_*ies 9

尝试添加到您的邮件属性文件somethig,如下所示:

typeMismatch.objectClassName.field=Your custom message

在你的情况下:

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

适合我:)