简体   繁体   中英

How does java bean validation work in practical

I was wondering have the java bean validation would work in practical, lets say we have a Maven mvc project, with a login form for a user. So we have a jsp/jsf page with the html forms, a Datamapper/DAO for the JDBC connection and a java User bean, which could look like this:

public class Student {

@NotNull(message ="username can't be null)
private String uName;
@NotNull(message ="lastname can't be null)
private String lname;
@Email (regex string="")
private String email;
private int age;

public Student(String uName, String lname, String email, int age) {
    this.uName = uName;
    this.lname = lname;
    this.email = email;
    this.age = age;
}

public String getuName() {
    return uName;
}

public void setuName(String uName) {
    this.uName = uName;
}

public String getLname() {
    return lname;
}

public void setLname(String lname) {
    this.lname = lname;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
    }
}

what would happen if a user typped in some not valid information in the view part of the application? What it go all the way down to the bean to get the message and then display it?

And how is the bean validation typically used? Only for Spring and Hibernate ,or for java EE in general?

Validation is used in JavaEE in general. Not only in Spring and Hibernate. Bean Validation is a JEE specification, marked as:

  • JSR-303 (v.1.0)
  • JSR-349 (v. 1.1)
  • JSR-380 (v. 2.0)

It works exacly as you mentioned in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM