sau*_*uce 7 spring annotations spring-mvc
我有一个对象.
public class MyObject
{
....
@Column(name = "a_number") @NotNull @NumberFormat(style = Style.NUMBER) @Min(1)
private Integer aNumber;
...
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我的对象被发布了@Valid注释.除了这个数字,我确实在类中的所有其他字段(它们的所有字符串)上进行验证.如果我从我的表单输入一个数字它工作正常,如果我违反了@Min(1)它也给了我正确的验证错误.但问题是,如果输入一个字符串而不是一个数字,它会抛出一个NumberFormatException.
我已经看过许多Integer和验证的例子,但如果你在发布的表单中输入一个字符串,则没有人会说明.我需要在其他位置进行验证吗?JavaScript的?我想要一个符合其余spring验证的解决方案,所以我可以在其他类中使用它.我只想说一个错误,声明它必须是数字.我也尝试使用@Pattern注释,但显然只是为了字符串.
建议?
您可以将以下内容添加到控制错误消息的文件中(这些是在类型不匹配的情况下它寻找的通用文件:
typeMismatch.commandObjectName.aNumber=You have entered an invalid number for ...
typeMismatch.aNumber=You have entered an invalid number for ...
typeMismatch.java.lang.Integer=You have input a non-numeric value into a field expecting a number...
typeMismatch=You have entered incorrect data on this page. Please fix (Catches all not found)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17251 次 |
| 最近记录: |