Spring MVC 驗證表單


   在實際工作中,得到數據后的第一步就是檢驗數據的正確性,如果存在錄入上的問題,一般會通過注解校驗,發現錯誤后返回給用戶,但是對於一些邏輯上的錯誤,比如購買金額=購買數量×單價,這樣的規則就很難使用注解方式進行驗證了,這個時候可以使用Spring所提供的驗證器(Validator)規則去驗證。
  所有的驗證都是要先注冊驗證器,不過驗證器也是SpringMVC自動加載的。這里筆者下載了關於驗證器所需的jar包,包括classmate-1.3.3.jar、jboss-logging-3.3.1.Final.jar、hibernate-validator-5.4.1.Final.jar和validation-api-1.1.0.Final.jar。其中,validation-api-1.1.0. Final.jar提供關於驗證注解的,它只有一些定義,而沒有實現;hibernate-validator-5.4.1. Final.jar是通過Hibernate檢驗規則的包,它的運行還依賴於classmate-1.3.3.jar和jboss-logging-3.3.1.Final.jar這兩個包。我們使用Hibernate檢驗規則把這些包加載進來。
<!-- 驗證-->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.4.1.Final</version>
</dependency>

 

使用JSR 303注解驗證輸入內容

  Spring提供了對Bean的功能校驗,通過注解@Valid標明哪個Bean需要啟用注解式的驗證。在javax.validation.con-straints.*中定義了一系列的JSR 303規范給出的注解,在使用它們之前需要對這些注解有一定的了解,如表所示。

 



  代碼清單15-35:交易表單
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>validate</title>
</head>

<body>
<%--<form action="../validate/annotation.do">--%>
<form action="../validate/validator.do">
    <table>
        <tr>
            <td>產品編號:</td>
            <td><input name="productId" id="productId" value="12"/></td>
        </tr>
        <tr>
            <td>用戶編號:</td>
            <td><input name="userId" id="userId" value="1231"/></td>
        </tr>
        <tr>
            <td>交易日期:</td>
            <td><input name="date" id="date" value="2019-06-30"/></td>
        </tr>
        <tr>
            <td>價格:</td>
            <td><input name="price" id="price" value="12323"/></td>
        </tr>
        <tr>
            <td>數量:</td>
            <td><input name="quantity" id="quantity" value="500"/></td>
        </tr>
        <tr>
            <td>交易金額:</td>
            <td><input name="amount" id="amount" value="3523"/></td>
        </tr>
        <tr>
            <td>用戶郵件:</td>
            <td><input name="email" id="email" value="email"/></td>
        </tr>
        <tr>
            <td>備注:</td>
            <td><textarea id="note" name="note" cols="20" rows="5">我的世界</textarea></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><input type="submit" value="提交"/>
        </tr>
    </table>
</form>
</body>
</html>

 


  代碼清單15-36:表單POJO
package com.ssm.chapter15.pojo;

import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.*;
import java.util.Date;

public class Transaction {

    // 產品編號
    @NotNull // 不能為空
    private Long productId;

    // 用戶編號
    @NotNull // 不能為空
    private Long userId;

    // 交易日期
    @Future // 只能是將來的日期
    @DateTimeFormat(pattern = "yyyy-MM-dd") // 日期格式化轉
    @NotNull // 不能為空
    private Date date;


    // 價格
    @NotNull // 不能為空
    @DecimalMin(value = "0.1") // 最小值0.1
    private Double price;

    // 數量
    @Min(1)  // 最小值為1
    @Max(100) // 最大值
    @NotNull // 不能為空
    private Integer quantity;

    // 交易金額
    @NotNull // 不能為空
    @DecimalMax("500000.00") // 最大金額為5萬元
    @DecimalMin("1.00") // 最小交易金額1元
    private Double amount;

    // 郵件
    @Pattern( // 正則式
            regexp = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@" + "([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.] [A-Za-z]{2})?$",
            // 自定義消息提示
            message = "不符合郵件格式")
    private String email;

    // 備注
    @Size(min = 0, max = 256)// 0到255個字
    private String note;

    @NotNull
    public Long getProductId() {
        return productId;
    }

    public void setProductId(@NotNull Long productId) {
        this.productId = productId;
    }

    @NotNull
    public Long getUserId() {
        return userId;
    }

    public void setUserId(@NotNull Long userId) {
        this.userId = userId;
    }

    @NotNull
    public Date getDate() {
        return date;
    }

    public void setDate(@NotNull Date date) {
        this.date = date;
    }

    @NotNull
    public Double getPrice() {
        return price;
    }

    public void setPrice(@NotNull Double price) {
        this.price = price;
    }

    @NotNull
    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(@NotNull Integer quantity) {
        this.quantity = quantity;
    }

    @NotNull
    public Double getAmount() {
        return amount;
    }

    public void setAmount(@NotNull Double amount) {
        this.amount = amount;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @Override
    public String toString() {
        return "Transaction{" +
                "productId=" + productId +
                ", userId=" + userId +
                ", date=" + date +
                ", price=" + price +
                ", quantity=" + quantity +
                ", amount=" + amount +
                ", email='" + email + '\'' +
                ", note='" + note + '\'' +
                '}';
    }
}

 

  代碼清單15-37:用控制器驗證表單
package com.ssm.chapter15.controller;

import com.ssm.chapter15.pojo.Transaction;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/validate")
public class ValidateController {

    @RequestMapping("/annotation")
    public ModelAndView annotationValidate(@Valid Transaction trans, Errors errors) {

        //是否存在錯誤
        if (errors.hasErrors()) {
            // 獲取錯誤信息
            List<FieldError> errorList = errors.getFieldErrors();
            for (FieldError error : errorList) {
                //打印字段錯誤信息
                System.err.println("fied :" + error.getField() + "\t" + "msg:" + error.getDefaultMessage());
            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
}

 


使用驗證器

  有時候除了簡單的輸入格式、非空性等校驗,也需要一定的業務校驗,Spring提供了Validator接口來實現檢驗,它將在進入控制器邏輯之前對參數的合法性進行檢驗。
  代碼清單15-39:交易驗證器
package com.ssm.chapter15.validator;

import com.ssm.chapter15.pojo.Transaction;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class TransactionValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {

        //判斷驗證是否為Transaction,如果是則進行驗證
        return Transaction.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Transaction trans = (Transaction) target;
        // 求交易金額和價格×數量的差額
        double dis = trans.getAmount() - (trans.getPrice() * trans.getQuantity());
        //如果差額大於0.01,則認為業務錯誤
        if (Math.abs(dis) > 0.01) {
            // 加入錯誤信息
            errors.rejectValue("amount", null, "交易金額和購買數量與價格不匹配");
        }
    }
}

 

  這樣這個驗證器就判斷了是否Transaction對象,如果是才去驗證后面的邏輯,那么要將它捆綁到對應的控制器中,這個時候Spring MVC提供了注解@InitBinder,后文我們會再次詳細講解@InitBinder。通過它就可以將驗證器和控制器捆綁到一起,這樣就能夠對請求表單進行驗證了。對於@InitBinder的使用還有其他的內容,這里只展示其捆綁驗證器的方法。

  代碼清單15-40:使用驗證器驗證
@InitBinder
public void initBinder(DataBinder binder) {

    //數據綁定器加入驗證器
    binder.setValidator(new TransactionValidator());
}

@RequestMapping("/validator")
public ModelAndView validator(@Valid Transaction trans, Errors errors) {
    //是否存在錯誤
    if (errors.hasErrors()) {
        // 獲取錯誤信息
        List<FieldError> errorList = errors.getFieldErrors();
        for (FieldError error : errorList) {
            // 打印字段錯誤信息
            System.err.println("fied :" + error.getField() + "\t" + "msg:" + error.getDefaultMessage());
        }
    }
    ModelAndView mv = new ModelAndView();
    mv.setViewName("index");
    return mv;
}

 

  比較遺憾的是,JSR 303注解方式和驗證器方式不能同時使用,不過可以在使用JSR 303注解方式得到基本的檢驗信息后,再使用自己的方法進行驗證。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM