我正在使用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"
有什么更好的点子吗?
以前从未做到了,但也许你可以在你的绑定类中添加try catch? –
@DanielRobertus请看下面的选择答案 – WowBow