spring-boot启动器中(spring-boot-starter-web)已经包含了validate的校验依赖jar包
完成这个表单校验只需要再引入thymeleaf的依赖和修改其版本信息即可。完整的pom文件:
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.jiayun</groupId>
<artifactId>spring-boot-hebrinate-validate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 修改jdk版本 -->
<properties>
<java.version>1.7</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
表单校验其实几个注解就搞定了
在实体对象类需要校验的属性上添加注解。
package com.jiayun.entity;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
public class User {
@NotBlank(message="登录名不能为空")
@Length(min=2,max=6,message="最小长度为2位,最大长度为6位")
private String loginName;
private String password;
private Integer age;
@Email
private String email;
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [loginName=" + loginName + ", password=" + password + ", age=" + age + ", email=" + email + "]";
}
}
在controller中接收参数上添加注解,标识注入该对象时候,对象的字段按照对象字段注解标识校验。并把校验结果作为controller的一个接收参数。
校验有问题时,会把有问题的提示封装成controller入参的实体对象塞进model中,页面直接调用跳转到显示页面,提示。
@RequestMapping("/add")
public String add(@Valid User user, BindingResult result){
if(result.hasErrors()){
// 校验失败,返回注册页面。spring 会自动把校验提示信息塞进model变量中,前台可以获取到
// 对于值的传递过程我也不是太理解。首先是变量的类型
return "register";// 注册页面
}
// 注册成功
System.out.println(user);
return "success";
}
启动类就不赘述了
thymeleaf页面的写法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form th:action="@{/add}" method="post">
登录名:<input name="loginName" ><span style="color:red;">*<span th:errors="${user.loginName}"></span></span><br/>
密码:<input name="password" ><span style="color:red;">*<span th:errors="${user.password}"></span></span><br/>
年龄:<input name="age" ><span style="color:red;">*<span th:errors="${user.age}"></span></span><br/>
邮箱:<input name="email" ><span style="color:red;">*<span th:errors="${user.email}"></span></span><br/>
<input name="save" type="submit" >
</form>
</body>
</html>