Data validation occupies an important place in the application development of Java hierarchy. Java EE 6 introduces the Bean Validation specification, which uses annotations to validate Java beans, not limited to a certain level or a programming model, and is flexible and easy to use
Bean Validation Specification
Beans are abbreviations for Java beans, and in the practical application of the Java layered architecture, from the presentation layer to the persistence layer, each layer requires business compliance validation for the Java bean, as shown in 1. For objects of the same Java Bean, however, this is a time-consuming and error-prone practice when you need to implement the same validation logic at each level. The goal of the Bean Validation specification is to avoid the repetition of multilayer validations. In fact, developers are more inclined to put validation rules directly into the Java Bean itself, using annotations to design validation rules.
Project Architecture
Pom.xml
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >< modelversion>4.0.0</modelversion><groupid>test</groupid><artifactid>vi</ Artifactid><version>0.0.1-snapshot</version><packaging>jar</packaging><name> vi</name><url>http://maven.apache.org</url><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding></properties><dependencies ><dependency><groupid>org.hibernate</groupid><artifactid>hibernate-validator</ Artifactid><version>5.1.3.final</version></dependency><dependency><groupid> javax.el</groupid><artifactid>el-api</artifactid><version>1.0</version></ Dependency></dependencies></project>
Loginform.java
Package Com.test;import Java.util.set;import Javax.validation.constraintviolation;import Javax.validation.validation;import Javax.validation.validator;import Javax.validation.validatorfactory;import Javax.validation.constraints.max;import Javax.validation.constraints.min;import Javax.validation.constraints.notnull;import Javax.validation.constraints.size;public class LoginForm { @NotNull (message = "User name cannot be empty") @Size (min = 2, max = nine, message = "User length 2-12 bits") &NBSP;PR Ivate String username; @NotNull (message = "Password cannot be null") @Size (min = 6, max = A, message = "Password length 6-12-bit") private String password; @Min (value =, message = "Minimum age must be 18") @Max (value = $, message = "Maximum age must be") private int age; public S Tring GetUserName () { return username; } public voidSetusername (String username) { this.username = username; } public String GetPassword () { return password; } public void SetPassword (String password) { This.password = password; } public int getage () { & Nbsp;return age; } public void setage (int age) { & Nbsp;this.age = age; } public static void Main (string[] args) { & nbsp; validatorfactory factory = validation.builddefaultvalidatorfactory (); validator Validator = Factory.getvalidator (); loginform loginform = New LoginForm (); loginform.setpassword ("1");nbsp; Set<ConstraintViolation<LoginForm>> constraintviolations = Validator.validate (loginform); for (constraintviolation<loginform> constraintviolation:constraintviolations) { System.out.println (Constraintviolation.getmessage ()); } system.out.println ("------"); loginform loginForm2 = new LoginForm (); loginform2.setpassword ("1234567"); Set<ConstraintViolation<LoginForm>> constraintViolations2 = validator.validate (loginForm2 ); for (constraintviolation<loginform> constraintviolation: ConstraintviolaTIONS2) { system.out.println ( Constraintviolation.getmessage ()); } }}
Run results
Java Data validation