zoukankan      html  css  js  c++  java
  • SpringMVC表单验证与Velocity整合

    阅读本文约“1.2分钟”

    定义表单类

    以Login为例,有username和password两个字段

    import javax.validation.constraints.NotNull;

    import javax.validation.constraints.Size;

    public class LoginForm {

    @Size(min=2,max=30,message="长度在2和30之间")

    @NotNull

    private String username;

    @NotNull(message="不能为空")

    private String password;

    public LoginForm(){}

    public LoginForm(String username,String password){

    this.username = username;

    this.password = password;

    }

    public String getUsername() {

    return username;

    }

    public void setUsername(String username) {

    this.username = username;

    }

    public String getPassword() {

    return password;

    }

    public void setPassword(String password) {

    this.password = password;

    }

    }

    属性上的注解,属于JSR-303规范,是JAVA EE 6中的一项子规范,称作Bean Validation.
    @NotNull代表字段不能为空,@Size定义字段字符长度,message定义返回的错误信息,不定义的话,返回默认错误信息。
    另外,还有很多
    1.@NotNull/@Null
    验证字段: 引用数据类型
    注解说明:注解元素必须是非空或空
    2.@Digits
    验证字段:byte、short、int、long及各自的包装类型以及BigDecimal、BigInteger、String
    注解说明:验证数字构成是否合法
    属性说明:integer:指定整数部分数字位数,fraction:指定小数部分数字位数
    3.@Future/Past
    验证字段:java.util.Date,java.util.Calendar
    注解说明:验证是否在当前系统时间之后/之前
    4.@Max/@Min
    验证字段:byte、short、int、long及对应的包装类型以及BigDecimal、BigInteger
    注解说明:验证值是否小于等于最大指定整数值或大于等于最小指定整数值
    5.@Pattern
    验证字段:String
    注解说明:验证字符串是否匹配指定的正则表达式
    属性说明:regexp:匹配的正则表达式,flags:指定Pattern.Flag的数值,表示正则表达式的选项
    6.@Size
    验证字段:String、Collection、Map和数组
    注解说明:验证元素大小是否在指定范围内
    属性说明:max:最大长度,min:最小长度,message:提示信息,默认:{constraint.size}
    7.@DecimalMax/@DecimalMin
    验证字段:byte、short、int、long及对应的包装类型以及BigDecimal、BigInteger、String
    属性说明:验证值是否小于等于最大指定小数值或大于等于最小指定小数值
    8.@Valid
    属性说明:验证值是否需要递归调用
    Hibernate Validator 附加的 constraint
    9.@Email 被注释的元素必须是电子邮箱地址
    10.@Length 被注释的字符串的大小必须在指定的范围内
    11.@NotEmpty 被注释的字符串的必须非空
    12.@Range 被注释的元素必须在合适的范围内

  • 相关阅读:
    nexus docker 私有镜像处理
    nexus 使用Raw Repositories 进行maven site 发布
    nexus && minio s3 存储私有镜像
    spring boot 使用spring.resources.static-locations 分离系统模版&&资源文件
    Tencent Server Web 安装试用
    docker could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
    Tencent Server Web(TSW) 腾讯开源的nodejs 基础设施
    Stream Processing 101: From SQL to Streaming SQL in 10 Minutes
    13 Stream Processing Patterns for building Streaming and Realtime Applications
    Siddhi cep java 集成简单使用
  • 原文地址:https://www.cnblogs.com/UncleCatMySelf/p/9276344.html
Copyright © 2011-2022 走看看