springBoot 数据校验
1配置pom
<?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 https://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>2.3.1.RELEASE</version>-->
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.study</groupId>
<artifactId>study-validate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>study-validate</name>
<description>Demo project for Spring Boot</description>
<!-- 使用版本2.0以上jdk要用1.8以上的版本-->
<properties>
<java.version>1.8</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> <!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
2创建启动类
/**
* SpringBoot启动类
*
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3编写实体类
public class User {
@NotBlank(message="用户名不能为空") //非空校验
@Length(min=2,max=6,message="最小长度为2位,最大长度为6位")
private String name;
// @NotBlank //密码非空校验
@NotEmpty
private String password;
@Min(value=15)
private Integer age;
@Email
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}
4编写控制层
/**
* SpringBoot 数据校验
*
*/
@Controller
public class UserController {
@RequestMapping("/addUser")
public String showPage(User user) {
return "add";
}
/*** 完成用户添加 */
@RequestMapping("/save")
public String saveUser(@Valid User user, BindingResult result) {
if (result.hasErrors()) {
return "add";
}
System.out.println(user);
return "ok";
}
}
4编写 add.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<form th:action="@{/save}" method="post">
用户姓名:<input type="text" name="name"/><font color="red" th:errors="${user.name}"></font><br/>
用户密码:<input type="password" name="password" /><font color="red" th:errors="${user.password}"></font><br/>
用户年龄:<input type="text" name="age" /><font color="red" th:errors="${user.age}"></font><br/>
用户邮箱:<input type="text" name="email" /><font color="red" th:errors="${user.email}"></font><br/>
<input type="submit" value="OK"/></form>
</body>
</html>
5编写 ok.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作成功</title>
</head>
<body>
OK。。。。
</body>
</html>