SpringMvc hibernate validator

    xiaoxiao2026-03-16  2

    validator(验证器)是作用于object级的。规范文档有两个,一个是JSR303 “Bean Validator”和JSR349 “Bean Validator 1.1”,它们都定义了一整套API。通过标注给对象的属性,来添加约束。也就是进行验证的规则。这两个约束可以从以下两个链接下载: JSR303 JSR349 但是JSR只是一个规范文档,我们需要的是它的实现。我这里用的是Hibernate Validator,它实现了JSR303和JSR349 JSR303不需要编写验证器类,只需要利用JSR303的标注类型指定约束 这里copy了一份JSR303的约束表

    空检查 @Null 验证对象是否为null @NotNull 验证对象是否不为null, 无法查检长度为0的字符串 @NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格. @NotEmpty 检查约束元素是否为NULL或者是EMPTY. Booelan检查 @AssertTrue 验证 Boolean 对象是否为 true @AssertFalse 验证 Boolean 对象是否为 false 长度检查 @Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内 @Length(min=, max=) Validates that the annotated string is between min and max included. 日期检查 @Past 验证 Date 和 Calendar 对象是否在当前时间之前 @Future 验证 Date 和 Calendar 对象是否在当前时间之后 @Pattern 验证 String 对象是否符合正则表达式的规则 数值检查,建议使用在Stirng,Integer类型,不建议使用在int类型上,因为表单值为“”时无法转换为int,但可以转换为Stirng为"",Integer为null @Min 验证 Number 和 String 对象是否大等于指定的值 @Max 验证 Number 和 String 对象是否小等于指定的值 @DecimalMax 被标注的值必须不大于约束中指定的最大值. 这个约束的参数是一个通过BigDecimal定义的最大值的字符串表示.小数存在精度 @DecimalMin 被标注的值必须不小于约束中指定的最小值. 这个约束的参数是一个通过BigDecimal定义的最小值的字符串表示.小数存在精度 @Digits 验证 Number 和 String 的构成是否合法 @Digits(integer=,fraction=) 验证字符串是否是符合指定格式的数字,interger指定整数精度,fraction指定小数精度。 @Range(min=, max=) Checks whether the annotated value lies between (inclusive) the specified minimum and maximum. @Range(min=10000,max=50000,message="range.bean.wage") private BigDecimal wage; @Valid 递归的对关联对象进行校验, 如果关联对象是个集合或者数组,那么对其中的元素进行递归校验,如果是一个map,则对其中的值部分进行校验.(是否进行递归验证) @CreditCardNumber信用卡验证 @Email 验证是否是邮件地址,如果为null,不进行验证,算通过验证。 @ScriptAssert(lang= ,script=, alias=) @URL(protocol=,host=, port=,regexp=, flags=)

    springmvc跟 hibernate validator整合十分简单 1、 在springmvc配置文件中有这一句

    <mvc:annotation-driven/>

    2、 把hibernate validator的jar包添加到classpath路径下,如果项目部署在tomcat上,那/web-inf/lib中要有这个jar包 3、 在需要验证的object添加@Valid,在具体需要约束的属性上面添加约束

    @RequestMapping(value = "/product_save") public String saveProduct(@Valid @ModelAttribute Product product, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { FieldError fieldError = bindingResult.getFieldError(); logger.info("Code:" + fieldError.getCode() + ", object:" + fieldError.getObjectName() + ", field:" + fieldError.getField()); return "ProductForm"; } // save product here model.addAttribute("product", product); return "ProductDetails"; } public class Product implements Serializable { private static final long serialVersionUID = 78L; @Size(min=1, max=10) private String name; @NotEmpty private String description; @NotNull private Float price; @Past private Date productionDate; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public Date getProductionDate() { return productionDate; } public void setProductionDate(Date productionDate) { this.productionDate = productionDate; } }

    4、 如果需要覆盖验证器的错误信息,只需在属性文件中添加property键 形式为:constraint.object.property=需要显示的错误信息

    Size.product.name = Product name must be 1 to 10 characters long Past.product.productionDate=Production date must a past date NotNull.product.price=价格不能为空

    实例: 验证失败网页上会显示验证失败的消息 项目连接:SpringMvcValidatorTest

    相关资源:python入门教程(PDF版)
    最新回复(0)