ore*_*ake 12 validation spring spring-mvc bean-validation
模型....
@Digits(integer=5, fraction=0, message="The value must be numeric and less than five digits")
private int value;
Run Code Online (Sandbox Code Playgroud)
豆文件....
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
控制器....
@RequestMapping(value = "/admin/save.htm", method = { RequestMethod.POST })
public ModelAndView saveSection(@Valid @ModelAttribute Section section, BindingResult result) {
if(result.hasErrors()) {
return new ModelAndView("admin/editSection", "section", section);
}
Run Code Online (Sandbox Code Playgroud)
如何将"价值"限制在数字范围内?如果我输入的不是数字,我会收到此错误:
无法将类型为java.lang.String的属性值转换为属性值所需的java.lang.Integer类型; 嵌套异常是org.springframework.core.convert.ConversionFailedException:无法将类型java.lang.String中的值"A"转换为java.lang.Integer类型; 嵌套异常是java.lang.IllegalArgumentException:无法解析A.
我看过一些提到initBinding的帖子,但我不确定如何使用它,或者它是否会帮助我.这必须在以前解决.有没有办法确保它在绑定之前是一个数字?
或者,如果有人可以发布正确的messages.properties条目来覆盖此错误,那也可能对我有用.
I tried @Pattern but that doesn't work on ints
axt*_*avt 25
正如您所提到的,您需要一个用户友好的消息messages.properties
.您可以使用以下消息代码之一(具有不同的选择性级别):
typeMismatch.section.value
typeMismatch.value
typeMismatch.int
typeMismatch
此外,当您不知道消息代码时,您可以简单地打印BindingResult
- 它toString()
返回绑定错误的完整描述.
归档时间: |
|
查看次数: |
7590 次 |
最近记录: |