Http://blog.sina.com.cn/s/blog_6829be5c0101alxh.html a very detailed explanation and worthy of further discussion
http://bbs.csdn.net/topics/360254599 Simple and clear
1. Import dependent Packages First Bean-validator.jar
2. Write some relevant validation information on the entity class: You can search for more verification methods, which is only part of the
can refer to: Click on open Link http://blog.csdn.net/caihaijiang/article/details/7463514
Private String username;
private String password;
Private String nickname;
private String Email;
Public user () {
}
@NotEmpty (message= "User name cannot be empty") public
String GetUserName () {return
username;
}
public void Setusername (String username) {
this.username = username;
}
@Size (min=1,max=10,message= "Password length should be between 1 and 10") Public
String GetPassword () {return
password;
}
public void SetPassword (String password) {
this.password = password;
}
Public String Getnickname () {return
nickname;
}
public void Setnickname (String nickname) {
this.nickname = nickname;
}
@Email (message= "Malformed mailbox") Public
String Getemail () {return
email;
}
public void Setemail (String email) {
This.email = email;
}
3. Background controller processing
@RequestMapping ("save") public
String Save (@Valid User user,bindingresult result) {
//
if ( Result.haserrors ()) {
list<objecterror> ls=result.getallerrors ();
for (int i = 0; i < ls.size (); i++) {
System.out.println ("Error:" +ls.get (i));
}
return "AddUser";
}
An in-depth analysis of Bindingresult: If you use the interface to return information, you can directly get the error message in Bindingresult
if (Br.haserrors ()) {
//If there is an error jump directly to the Add view
fielderror fielderror= br.getfielderror ();
System.out.println (Fielderror.getdefaultmessage ());
list<fielderror> list = Br.getfielderrors ();
for (Fielderror fielderror2:list) {
System.out.println (fielderror2.getdefaultmessage ());
}
List<objecterror> ls=br.getallerrors ();
for (int i = 0; i < ls.size (); i++) {
System.out.println ("Error:" +ls.get (i). Getdefaultmessage ());
return "User/add";
}