我正在使用 Spring MVC 2.5 。
我有一些字段,其中数字只能被允许输入。我在用户界面上得到了我正在寻找的确切错误消息。类似的东西
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"
还有更好的想法吗?
最佳答案
尝试在消息属性文件中添加如下内容:
typeMismatch.objectClassName.field=您的自定义消息
就您而言:
typeMismatch.offerSetting.amount=您输入的字段价格金额无效
对我有用:)
关于java - 覆盖 BindingResult 中的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076142/