Spring MVC中修改校验的异常信息

使用一个例子来说明一下,Spring使用的是3.0.x:

public class Account {
@Pattern(regexp = "[a-z0-9]{3,50}")
private String name;

@NotNull
@NumberFormat(style=Style.CURRENCY)
private BigDecimal balance = new BigDecimal("1000");

@DateTimeFormat(style="S-")
@Future
private Date renewalDate = new Date(new Date().getTime() + 31536000000L);

// 省略 getter & setter...
}

接下来使用注解创建一个Controller:

@Controller
@RequestMapping(value="/account")
public class AccountController {

/**
* 如果view采用JSP的话@ModelAttribute注解可以省略,但是如果是使用Velocity或FreeMarker
* 的话必须要加上,而且@Valid注解和后面的BindingResult类型的参数之间不能有其他参数。
*/
@RequestMapping(method=RequestMethod.POST)
public String create(@Valid @ModelAttribute("account") Account account, BindingResult result) {
if (result.hasErrors()) {
return "account/createForm";
}
// ...
return "redirect:/account/" + account.getId();
}

}

另外还有一个view文件、配置文件等等,具体的可以查看Spring MVC Basic Sample

OK,接下来说重点。我参考上面的Spring MVC Basic Sample中的做法也做了一个类似的表单,在表单中的balance中输入非数字的时候旁边的错误信息会出现下面的异常信息:

Failed to convert property value of type java.lang.String to required type java.math.BigDecimal for property balance; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "eff" from type java.lang.String to type java.math.BigDecimal; nested exception is java.lang.IllegalArgumentException: Unable to parse eff

这个就很郁闷了,明显是格式转换出错,但是列出来一堆的异常信息,对用户来说非常不友好。于是开始在Google中搜索也没有找到比较好的说法,后来运行了Spring MVC Basic Sample,不显示上面的异常信息,而只是显示一句话:"could not be parsed“,这个效果正是我们所期待的。于是我将例子中的所有spring配置文件、Bean信息、Controller都做了对比,甚至JSP模板都一样,但是一到我自己的项目中就会出现那个异常,当时非常郁闷。

后来无意中在Google中查到到了参考资料中的一篇帖子,里面提到只要在messages.properties文件中设置typeMismatch属性就可以了,于是我猛然想到Spring MVC Basic Sample中也有个messages.properties文件,打开之后赫然看到果然有一条typeMismatch属性:

typeMismatch=could not be parsed

原来是在这里设置的,真的很郁闷。还好,参考资料中的帖子中还提到可以设置具体的类型,比如可以这样设置:

## 全局的配置信息
typeMismatch=输入的数据格式不正确
## 针对BigDecimal类型的错误信息
typeMismatch.java.math.BigDecimal=请输入数字
## 针对Date类型的错误信息
typeMismatch.java.util.Date=请在{0}中输入正确的日期

参考资料:
Spring MVC Data Binding and user-friendly error messages

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注