Spring MVC-07循序渐进之验证器 下 (JSR 303验证)

  • 概述
  • JSR 303 Validator Demo
  • 测试
  • 总结
  • 源码

概述

JSR303”Bean Validation” 和 JSR349 “Bean Validation 1.1”指定了一整套的API,通过标注对象属性添加约束。

当然了JSR 只是一个规范文档,目前有两个实现

  • Hibernate Validator
  • Apache BVal (仅实现了JSR303)

这里我们使用 Hibernate Validator来演示

JSR303 不需要编写验证器,但是要利用JSR30标注类型嵌入约束.

JSR 303约束如下

属性 描述
@AssertFalse 用于boolean属性,必须为false
@AssertTrue 用于boolean属性,必须为true
@DecimalMax(value) 该属性值必须为一个不大于指定值的小数
@DecimalMin(value) 该属性值必须为一个不小于指定值的小数
@Digits 该属性值必须在指定的范围内, integer属性定义该数字的最大整数部分,fraction属性定义该数值的最大小数部分
@Future 该属性值必须是未来的一个日期
@Max 该属性值必须是一个小于或者等于指定值的整数
@Min 该属性值必须为大于或者等于指定值的整数
@NotNull 该属性值不能为null
@Null 该属性值必须为null
@Past 该属性值必须是过去的一个日期
@Pattern 该属性值必须与指定的常规表但是相匹配
@Size 该属性值必须在指定范围内
….等

一旦了解了JSR303 validation的使用方法,使用起来比Spring验证器还要容易。 同使用Spring验证器一样,同样可以在属性文件中以下列格式使用property键来覆盖来自JSR303验证器的错误消息

constraint.object.property

JSR 303 Validator Demo

Spring MVC-07循序渐进之验证器 下 (JSR 303验证)_第1张图片

同Spring Validator不同在于,它没有ProductValidator类,其次,需要添加Maven依赖

<dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-validatorartifactId>
            <version>5.4.1.Finalversion>
dependency>

Product 类中标注了JSR 303注解

package com.artisan.domain;

import java.io.Serializable;
import java.util.Date;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;

public class Product implements Serializable {

    private static final long serialVersionUID = -5379168879247929742L;


    @Size(min=1,max=10)
    private String name;

    private String description;
    private float price;

    @NotNull
    @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;
    }

}

在ProductController类的productSave方法中,必须用@Valid对Product参数进行标注

package com.artisan.controller;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.artisan.domain.Product;

@Controller
@RequestMapping(value = "/product")
public class ProductController {

    private static final Logger logger = Logger.getLogger(ProductController.class);

    @RequestMapping(value = "/product_input", method = RequestMethod.GET)
    public String productInput(Model model) {
        model.addAttribute("product", new Product());
        return "ProductForm";
    }

    /**
     * 
    * @Title: productSave  
    * @Description: 标注了@Valid 对product进行校验
    * @param @param product
    * @param @param bindingResult
    * @param @param model
    * @param @return    参数  
    * @return String    返回类型  
    * @throws
     */
    @RequestMapping(value = "/product_save", method = RequestMethod.POST)
    public String productSave(@Valid @ModelAttribute Product product, 
            BindingResult bindingResult, Model model) {
        // 校验
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            logger.info("Code:" + fieldError.getCode() + " ,field:" + fieldError.getField());
            return "ProductForm";
        }

        // simulate save product here

        model.addAttribute("product", product);
        model.addAttribute("message", "add successfully");
        return "ProductView";
    }

}

为了定制来自验证器的错误消息,要在messages.properties文件中使用两个键

Size.product.name=Product name must be in 1 to 10 characters long
Past.product.productionDate=Production date must a past date
NotNull.product.productionDate=Production date must not be null

测试

什么都不输入

Spring MVC-07循序渐进之验证器 下 (JSR 303验证)_第2张图片

输入一个将来的时间

Spring MVC-07循序渐进之验证器 下 (JSR 303验证)_第3张图片

可见JSR 303 验证起了作用。


总结

由于JSR 303是正式的Java规范,因此建议新的项目使用JSR 303 验证器


源码

代码已提交到github

https://github.com/yangshangwei/SpringMvcTutorialArtisan

你可能感兴趣的:(【Spring-MVC】,Spring-MVC手札)