springMVC之JSR 303注解表单验证

1.JSR 303表单验证
这种方法比较适合银行类型使用的一些项目,涉及的安全度比较高时使用。所以,只要能看懂,会用就可以了。一般的项目中用不到

2.详细信息


image.png

3.需要的jar包:


image.png

4.实例

Validate实体类: 主要看注解

 package com.gb.pojo;

import java.util.Date;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.springframework.format.annotation.DateTimeFormat;

public class Validate {


@NotNull
private Integer id;


@NotNull
@DecimalMin("1.0")// 最小值
@DecimalMax("300000.0")// 最大值
private Double price;


@NotNull
@Past
@DateTimeFormat(pattern="yyyy-MM-dd")//可以解析的日期格式
private Date date;

@NotNull
@Min(1)
@Max(300)
private Integer value;




@Size(min=10,max=200)
private String note;


@Pattern(regexp = "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$", message="邮箱格式不正确")
private String email;


public String getEmail() {
    return email;
}


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


public Integer getId() {
    return id;
}


public void setId(Integer id) {
    this.id = id;
}


public Double getPrice() {
    return price;
}


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


public Date getDate() {
    return date;
}


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


public Integer getValue() {
    return value;
}


public void setValue(Integer value) {
    this.value = value;
}


public String getNote() {
    return note;
}


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

jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here



ID:
价格:
日期:
值 :
备注:
邮箱:

ValidateController.java

 package com.gb.controller;

 import java.util.List;

 import javax.validation.Valid;

 import org.springframework.stereotype.Controller;
 import org.springframework.validation.Errors;
 import org.springframework.validation.FieldError;
 import org.springframework.web.bind.annotation.RequestMapping;

 import com.gb.pojo.Validate;

 @Controller
 @RequestMapping("val")
 public class ValidateController {

@RequestMapping("validate")
public String validate(@Valid Validate validate, Errors errors) {
    
    List fieldErrors = errors.getFieldErrors();
    
    // 得到验证的错误信息,之后可以加入自己的处理逻辑,此处只是将它们输出
    for(FieldError fe:fieldErrors) {
        System.out.println(fe.getField() + ", " + fe.getDefaultMessage());
    }
    
    return "test";
    
    
}}

test.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here



<%=session.getAttribute("id") %>
<%=session.getAttribute("name") %>
<%=session.getAttribute("note") %>
<%=session.getAttribute("role") %>

你可能感兴趣的:(springMVC之JSR 303注解表单验证)