当我尝试在课堂上进行“绑定”时,它会引发异常,但是如何在表单上显示错误?
控制器:
@InitBinder
public final void binder(WebDataBinder binder) {
binder.registerCustomEditor(Telefone.class, new PropertyEditorSupport(){
@Override
public void setAsText(String value){
if(null != value){
try{
setValue(Telefone.fromString(value));
} catch (IllegalArgumentException e) {
// what to do here ??
}
}
}
});
电话:
public static Telefone fromString(String s) {
checkNotNull(s);
String digits = s.replaceAll("\\D", "");
checkArgument(digits.matches("1\\d{2}|1\\d{4}|0300\\d{8}|0800\\d{7,8}|\\d{8,13}"));
return new Telefone(digits);
}
chekArgument 来自 Google Preconditions
当电话无效时,会抛出 IllegalArgumentException .. 但是如何将其放入 BindingResult