1.pom.xml 文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.daqu</groupId>
<artifactId>springboot_7</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_7</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.7.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建实体类 domian层 Users.java
@NotBlank: 非空校验
@NotBlank: 判断字符串是否为 null 或者是空串(去掉首尾空格)。
@NotEmpty: 判断字符串是否 null 或者是空串。
@Length: 判断字符的长度(最大或者最小)
@Min: 判断数值最小值
@Max: 判断数值最大值
@Email: 判断邮箱是否合法
这几个校验就是 我们今天的重点.
public class Users {
@NotBlank //非空校验
private String name;
@NotBlank //非空校验
private String password;
private Integer age;
@Override
public String toString() {
return "Users{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
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;
}
}
controller 层创建UsersController.java
@Controller
public class UsersController {
@RequestMapping("/addUser")
public String showPage(@ModelAttribute("aa") Users users){
return "add";
}
/*
*
* 需求: 如果没有填写相关信息 页面不跳转*/
@RequestMapping("/save")
public String saveUser (@ModelAttribute("aa") @Valid Users users, BindingResult result){
if (result.hasErrors()){
return "add";
}
System.out.println(users);
return "ok";
}
}
3.启动器
@SpringBootApplication
public class Springboot7Application {
public static void main(String[] args) {
SpringApplication.run(Springboot7Application.class, args);
}
}
4.页面 当页面选框填写为空就会一直返回到输入页面. 输入正确的信息,就跳转到添加成功的页面.
template下创建
add.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<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="${aa.name}"></font>
用户密码: <input type="text" name="password"/><font color="red" th:errors="${aa.password}"></font>
用户年龄: <input type="text" name="age"/><font color="red" th:errors="${aa.age}"></font>
<input type="submit" value="ok"/>
</form>
</body>
</html>
ok.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加成功</title>
</head>
<body>
添加成功
</body>
</html>