我正在使用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