表单校验分类
校验位置:
客户端校验
服务端校验
表单校验框架
JSR:java规范提案
303:提供bean属性相关校验规则
JCP:java社区
Hibernate框架中包含一套独立的校验框架hibernate-validator
实际的校验规则
同一个字段有多个约束条件
引用类型字段如何校验
根据业务不同需要调查是否参与校验
addemployee.jsp
<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<html>
<head>
<title>添加员工-用于演示表单验证</title>
</head>
<body>
<form action="/addemployee" method="post">
<%--页面使用${}获取后台传递的校验信息--%>
员工姓名:<input type="text" name="name"><span style="color:red">${name}</span><br/>
员工年龄:<input type="text" name="age"><span style="color:red">${age}</span><br/>
<%--注意,引用类型的校验未通过信息不是通过对象进行封装的,直接使用对象名.属性名的格式作为整体属性字符串进行保存的,和使用者的属性传递方式有关,不具有通用性,仅适用于本案例--%>
省:<input type="text" name="address.provinceName"><span style="color:red">${requestScope['address.provinceName']}</span><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp
<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<h1>数据添加成功</h1>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.itheima"/>
<mvc:annotation-driven />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
EmployeeController.java
package com.itheima.controller;
import com.itheima.controller.groups.GroupA;
import com.itheima.domain.Employee;
import javafx.scene.GroupBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
import java.util.List;
@Controller
public class EmployeeController {
@RequestMapping(value = "/addemployee")
public String addEmployee(@Validated(value = {GroupA.class}) Employee employee, Errors errors, Model model){ ///对他开启校验
System.out.println(errors.hasErrors());
if(errors.hasErrors()){
//获取所有未通过校验规则的信息
List<FieldError> fieldErrors = errors.getFieldErrors();
System.out.println(fieldErrors.size());
for(FieldError error : fieldErrors){
///name
//姓名不能为空
System.out.println(error.getField());
System.out.println(error.getDefaultMessage());
//将校验结果信息添加到Model对象中,用于页面显示,后期实际开发中无需这样设定,返回json数据即可
model.addAttribute(error.getField(),error.getDefaultMessage());
}
//当出现未通过校验的字段时,跳转页面到原始页面,进行数据回显
return "addemployee.jsp";
}
return "success.jsp";
}
@RequestMapping(value = "/addemployee2")
public String addEmployee2(@Valid Employee employee,Errors errors, Model model){ ///对他开启校验
System.out.println(errors.hasErrors());
if(errors.hasErrors()){
//获取所有未通过校验规则的信息
List<FieldError> fieldErrors = errors.getFieldErrors();
System.out.println(fieldErrors.size());
for(FieldError error : fieldErrors){
///name
//姓名不能为空
System.out.println(error.getField());
System.out.println(error.getDefaultMessage());
//将校验结果信息添加到Model对象中,用于页面显示,后期实际开发中无需这样设定,返回json数据即可
model.addAttribute(error.getField(),error.getDefaultMessage());
}
//当出现未通过校验的字段时,跳转页面到原始页面,进行数据回显
return "addemployee.jsp";
}
return "success.jsp";
}
}
Address.java
package com.itheima.domain;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
//嵌套校验的实体中,对每个属性正常添加校验规则即可
public class Address {
@NotBlank(message = "请输入省份名称")
private String provinceName;//省份名称
@NotBlank(message = "请输入城市名称")
private String cityName;//城市名称
@NotBlank(message = "请输入详细地址")
private String detail;//详细住址
@NotBlank(message = "请输入邮政编码")
@Size(max = 6,min = 6,message = "邮政编码由6位组成")
private String zipCode;//邮政编码
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
@Override
public String toString() {
return "Address{" +
"provinceName='" + provinceName + '\'' +
", cityName='" + cityName + '\'' +
", detail='" + detail + '\'' +
", zipCode='" + zipCode + '\'' +
'}';
}
}
Employee.java
package com.itheima.domain;
import com.itheima.controller.groups.GroupA;
import org.hibernate.validator.constraints.Range;
import javax.validation.Valid;
import javax.validation.constraints.*;
public class Employee{
//设定校验器,设置校验不通过对应的消息,设定所参与的校验组
@NotBlank(message = "姓名不能为空",groups = {GroupA.class})
private String name;//员工姓名
@NotNull(message = "请输入您的年龄",groups = {GroupA.class})
@Max(value = 60,message = "年龄最大值不允许超过60岁")
@Min(value = 18,message = "年龄最小值不允许低于18岁")
private Integer age;//员工年龄
@Valid
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
GroupA.java
package com.itheima.controller.groups;
//用于设定分组校验中的组名,当前接口仅提供字节码,用于识别
public interface GroupA {
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springmvc_validator</groupId>
<artifactId>springmvc_validator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- servlet3.0规范的坐标 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!--spring的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--springmvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--导入校验的jsr303规范-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!--导入校验框架实现技术-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
</dependencies>
<build>
<!--设置插件-->
<plugins>
<!--具体的插件配置-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>