SpringBoot 中使用了 Hibernate-validate 校验框架
校验规则
- @NotBlank: 判断字符串是否为 null 或者是空串(去掉首尾空格)。
- @NotEmpty: 判断字符串是否 null 或者是空串。
- @Length: 判断字符的长度(最大或者最小)
- @Min: 判断数值最小值
- @Max: 判断数值最大值
- @Email: 判断邮箱是否合法
SpringBoot 表单数据校验步骤
1.在实体类中添加校验规则
public class Users {
@NotBlank(message = "用户名不能为空")
private String name;
@Min(12)
@NotNull
private Integer age;
private String password;
/*getter and setter*/
}
2.在 Controller 中开启校
package com.zth.controller;
import com.zth.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
/**
* @author zth
* @Date 2019-07-25 11:25
*/
@Controller
public class UsersController {
/**
* 页面跳转
*/
@RequestMapping("/addUser")
public String showPage(){
return "add";
}
/**
* 添加用户
* @Valid 开启对 Users 对象的数据校验
* @param result 封装了校验的结果
*/
@RequestMapping("/save")
public String addUser(@Valid Users users, BindingResult result){
if (result.hasErrors()){
return "add";
}
System.out.println(users);
return "ok";
}
}
3. 在页面中获取提示信息
add.html: 提交添加用户表单
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<center>
<form th:action="@{/save}" method="post">
用户姓名:<input type="text" name="name"/>
<font color="red" th:errors="${users.name}"></font><br/>
用户年龄:<input type="text" name="age" />
<font color="red" th:errors="${users.age}"></font><br/>
用户密码:<input type="password" name="password" /><br>
<input type="submit" value="OK"/>
</form>
</center>
</body>
</html>
ok.html: 操作成功页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
操作成功!!!
</body>
</html>
4. 遇到异常
5. 解决方法
在跳转页面的方法中注入一个对象,来解决问题。要求参数对象的变量名必须是对象的类名的全称首字母小写。
/**
* 页面跳转
* 由于 springmvc 会将该对象放入到 Model 中传递。
* key 的名称会使用该对象的驼峰式的命名规则来作为 key。
* 参数的变量名需要与对象的名称相同。将首字母小写
*/
@RequestMapping("/addUser")
public String showPage(Users user){
return "add";
}
6. 运行效果:
7. 改变参数的名称
使用 @ModelAttribute("aa")
package com.zth.controller;
import com.zth.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
/**
* @author zth
* @Date 2019-07-25 11:25
*/
@Controller
public class UsersController {
/**
* 页面跳转
* 由于 springmvc 会将该对象放入到 Model 中传递。
* key 的名称会使用该对象的驼峰式的命名规则来作为 key。
* 参数的变量名需要与对象的名称相同。将首字母小写
*
* 如果想为传递的对象更改名称,可以使用@ModelAttribute("aa")这表示当
* 前传递的对象的 key 为 aa。
*/
@RequestMapping("/addUser")
public String showPage(@ModelAttribute("aa") Users user){
return "add";
}
/**
* 添加用户
* @Valid 开启对 Users 对象的数据校验
* @param result 封装了校验的结果
*/
@RequestMapping("/save")
public String addUser(@ModelAttribute("aa") @Valid Users users, BindingResult result){
if (result.hasErrors()){
return "add";
}
System.out.println(users);
return "ok";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<center>
<form th:action="@{/save}" method="post">
用户姓名:<input type="text" name="name"/>
<font color="red" th:errors="${aa.name}"></font><br/>
用户年龄:<input type="text" name="age" />
<font color="red" th:errors="${aa.age}"></font><br/>
用户密码:<input type="password" name="password" /><br>
<input type="submit" value="OK"/>
</form>
</center>
</body>
</html>