我正在学习Spring MVC 4.0,我在一个字段中显示错误消息时遇到了问题:
当我将文本框留空时,它有多个错误,如“密码不能为空”和 “Passwod长度必须在8到32个字符之间”,我只想显示第一条消息,但它显示所有消息。如何控制只有第一条消息会在有很多错误时显示给用户?
我尝试过定义一个自定义验证程序,但在使用@ReportAsSingleViolation
时只显示一条错误消息以下是我的代码:
型号:
public class User {
@NotEmpty
@Email
private String email;
@NotEmpty(message = "Password must not be blank")
@Size(min = 8, max = 32, message = "Passwod lenght must be between 8 and 32 characters")
//@CustomValidator(message = "Passwork must not be blank") //work well but doesn't meet my requirement
private String password;
@NotEmpty(message = "Please, enter your age")
@NumberFormat
@Min(value = 1, message = "Please, enter age > 0")
@Max(value = 100, message = "Please, enter age <= 100")
private String age;
以下是我的源代码演示:https://drive.google.com/file/d/0B_siKWi32VR9U2FnYUc2ZFByLWc/view?usp=sharing
全部谢谢!