006. SpringBoot(1.5.10版本)服务端表单数据校验(踏入高级部分有图)

在这里插入图片描述

表单数据校验

实现添加用户功能基础可以不看

  1. 创建项目
    在这里插入图片描述
  2. 修改 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.bjsxt</groupId>
<artifactId>13-spring-boot-validate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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.ve
rsion>
</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>
  1. 编写添加用户功能创建实体类
public class Users {
private String name;
private String password;
private Integer 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;
} 
@Override
public String toString() {
return "Users [name=" + name + ", password=" + password + ", age="
+ age + "]";
}}
  1. 编写 Controller
/**
* SpringBoot 表单数据校验
* * *
/
@Controller
public class UsersController {
@RequestMapping("/addUser")
public String showPage(){
return "add";
} 
//showPage()这么写会报错,他跳到的页面需要一个对象 这里没有所以报错
/
**
* 完成用户添加
*/
@RequestMapping("/save")
public String saveUser(Users users){
System.out.println(users);
return "ok";
}}
  1. 编写页面 add.html ok.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<form th:action="@{/save}" method="post">
用户姓名: <input type="text" name="name"/><br/>
用户密码: <input type="password" name="password" /><br/>
用户年龄: <input type="text" name="age" /><br/>
<input type="submit" value="OK"/>
</form>
</body>
</html>



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作成功</title>
</head>
<body>
OK。。。。
</body>
</html>

SpringBoot 对表单做数据校验

SpringBoot 对表单数据校验的技术特点

SpringBoot 中使用了 Hibernate-validate 校验框架

SpringBoot 表单数据校验步骤

在实体类中添加校验规则

public class Users {
@NotBlank //非空校验
private String name;
@NotBlank //密码非空校验
private String password;
private Integer age;
public String getName() {
return name;
}
set/get 省略
}

在 Controller 中开启校验*****

/**
* 完成用户添加
*@Valid 开启对 Users 对象的数据校验
*BindingResult:封装了校验的结果
*/
@RequestMapping("/save")
public String saveUser(@Valid Users users,BindingResult result){
if(result.hasErrors()){
return "add";
} 
System.out.println(users);
return "ok";
}

在页面中获取提示信息

<!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="${users.name}"></font> 
*****************
获取错误信息 users--控制层对 对象驼峰式命名 名字可以改如下图
*****************
<br/>

用户密码: <input type="password" name="password" />
<font  color="red" th:errors="${users.password}"></font><br/>

用户年龄: <input type="text" name="age" />
<font color="red" th:errors="${users.age}"></font><br/>
<input type="submit" value="OK"/>
</form>
</body>
</html>

在这里插入图片描述

其他校验规则

@NotBlank: 判断字符串是否为 null 或者是空串(去掉首尾空格)。
@NotEmpty: 判断字符串是否 null 或者是空串。
@Length: 判断字符的长度(最大或者最小)
@Min: 判断数值最小值
- @Min(value = 15)
@Max: 判断数值最大值
@Email: 判断邮箱是否合法

@NotBlank(message = “用户名不能为空”) //非空校验
@Length(min = 2,max = 6,message = “最小长度为2位,最大长度为6位”)

发布了162 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39088066/article/details/103563592
006
今日推荐