POJO how to add validation rules and validation message prompts?

Add validation rules

 1 @null annotated element must be null

2 @NotNull annotated element is not null

. 3 the @NotEmpty is non-empty string must be annotated 

 

 Verify that the message prompt

. 1  @AssertTrue the element must be annotated to true
 2  @AssertFalse must be annotated element to false
 . 3  @min (value) must be annotated element is a number whose value must be greater than the specified minimum
 . 4  @max (value) Note the element must be a number whose value is equal to the specified maximum value must be less than
 . 5  @DecimalMin (value) must be annotated element is a number whose value must be greater than the specified minimum
 . 6  @DecimalMax (value) are annotated It must be a number of elements which must be less than the maximum value equal to the specified
 7  size @Size (max, min) annotated element must be within the specified range.
. 8  @digits (Integer, fraction) annotated element must be a number whose value must be within an acceptable range
 . 9  @Past annotated element must be a date in the past
 10  @Future annotated element must be a future date
 11  @Pattern (value) annotated element must match the specified regular expression.
12 is  @email annotated element must be an email address
 13  within @Length size string must be annotated in a specified range
 14 @Range element to be annotated in the range of appropriate

Examples

. 1  Package com.hzy.model;
 2  
. 3  Import javax.validation.constraints.Email;
 . 4  Import javax.validation.constraints.Min;
 . 5  Import javax.validation.constraints.NotEmpty;
 . 6  Import javax.validation.constraints.NotNull;
 . 7  Import javax.validation.constraints.Size;
 . 8  
. 9  // model model class (POJO) 
10  public  class the User {
 . 11      
12 is      / ** 
13 is        * @ Min (value) must be annotated element is a number whose value must be greater than or equal specified minimum 
 14        * @ NotNull annotated element is not null
 15       * @ NotEmpty annotated string must not be null
 16        is sized annotated elements * @ Size (max, min) must be within the specified range.
. 17        * @ In Email annotated element must be e-mail address * / 
18 is      
. 19      
20 is      @NotNull // ID may not be blank 
21 is      Private  Long ID;
 22 is      
23 is      @NotNull // name can not be blank
 24      @ longest name is 30 , a minimum of two characters, if less than 2 more than 30 characters, the value of the message prompting 
25      @Size (= 2 min, max = 30, message = "name of between 2 to 30 characters" )
 26 is      Private name String;
 27      
28      @NotNull // Age can not be empty
 29      // Age equal to or greater than 15 years, if less than 15 years, the value of the message prompting
30      @min (Message = "Age at least 15 years old", value = 15 )
 31 is      Private Integer Age;
 32      
33 is      the @NotEmpty // email nonempty
 34      // whether the email address is correct determination 
35      @email (Message = "Mailbox wrong format " )
 36      Private String In Email;
 37 [  
38 is      public the User () {    
 39      }
 40  
41 is      public the User (@NotNull Long ID, @NotNull @Size (= 2 min, max = 30 ) String name,
 42 is              @NotNull @min (15 ) Age Integer, String In Email) {
 43 is          the this .id = id;
44         this.name = name;
45         this.age = age;
46         this.email = email;
47     }
48 
49     public long getId() {
50         return id;
51     }
52 
53     public void setId(long id) {
54         this.id = id;
55     }
56 
57     public String getName() {
58         return name;
59     }
60 
61     public void setName(String name) {
62         this.name = name;
63     }
64 
65     public Integer getAge() {
66         return age;
67     }
68 
69     public void setAge(Integer age) {
70         this.age = age;
71     }
72 
73     public String getEmail() {
74         return email;
75     }
76 
77     public void setEmail(String email) {
78         this.email = email;
79     }
80 }

 

Guess you like

Origin www.cnblogs.com/hzyhx/p/11105076.html