当前位置:网站首页>@Detailed explanation of valid annotation usage

@Detailed explanation of valid annotation usage

2022-06-25 06:18:00 WD Technology

First of all we have Employee The properties of the class are annotated as follows :

package com.zyq.beans;
 
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
 
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
 
/**
 *  The employee object 
 * 
 * @author sunnyzyq
 * @since 2019/12/13
 */
public class Employee {
 
    /**  full name  */
    @NotBlank(message = " Please enter name ")
    @Length(message = " Names cannot exceed  {max}  character ", max = 10)
    public String name;
 
    /**  Age  */
    @NotNull(message = " Please enter age ")
    @Range(message = " The age range is  {min}  To  {max}  Between ", min = 1, max = 100)
    public Integer age;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
}

And then again Controller On the corresponding method , Mark the employee with @Valid annotation , Indicates that we need to verify the properties of this object
 Insert picture description here

原网站

版权声明
本文为[WD Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201237011383.html