1.SpringBoot 对表单数据校验的技术特点
1.1SpringBoot 中使用了 Hibernate-validate (现在已经被javax.validation.constraints数据校验取代)校验框架(springboot中默认已经集成,无需手动引入约束)
2.SpringBoot 表单数据校验步骤
2.1在实体类中添加校验规则
package com.example.demo.pojo;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import org.hibernate.validator.constraints.Length;
public class Custom {
@Length(max=5,min=2,message = "为2-5个字符!")
@NotBlank(message = "用户名不能为空!请重新填写!")
private String sname;
@NotBlank(message = "密码为空!!!")
private String passwords;
@Max(value = 100,message = "年龄最大为100")
@Min(value = 18,message = "年龄最小为18")
private Integer age;
public Custom() {
super();
// TODO Auto-generated constructor stub
}
public Custom(String sname, String passwords, Integer age) {
super();
this.sname = sname;
this.passwords = passwords;
this.age = age;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getPasswords() {
return passwords;
}
public void setPasswords(String passwords) {
this.passwords = passwords;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Custom [sname=" + sname + ", passwords=" + passwords + ", age=" + age + "]";
}
}
校验注解:
@NotBlank | 判断字符串是否为 null 或者是空串(去掉首尾空格) |
@NotEmpty | 判断字符串是否 null 或者是空串 |
@Length | 判断字符的长度(最大或者最小) |
@Min | 判断数值最小值 |
@Max | 判断数值最大值 |
判断邮箱是否合法 |
2.2在contorller中开启校验
package com.example.demo.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.pojo.Custom;
import com.example.demo.service.CustomService;
@Controller
public class CustomController {
@Autowired
private CustomService customService;
@RequestMapping("/addCustom")
/**
*
*需要在跳转页面的方法中注入一个 Custom 对象,否则将出现异常。
*注意:由于 SpringMVC 会将该对象放入到 Model 中传递。key 的名称会使用
*该对象的驼峰式的命名规则来作为 key。这就要求参数的变量名需要与对象的名称相同。
*并且要求参数对象的变量名必须是对象的类名的全称首字母小写。
*/
public String showPage(Custom custom) {
return "insert";
};
@RequestMapping("/inserts")
/**
*
* @Valid 开启对 Users 对象的数据校验
* BindingResult:封装了校验的结果
*/
public String insertCustom(@Valid Custom custom,BindingResult result) {
if(result.hasErrors()) {
return "insert";
}else {
customService.insertCustom(custom);
return "ok";
}
}
}
2.3在页面中开启信息提示
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/inserts}" method="post">
用户姓名:<input type="text" name="sname"/><font color="red" th:errors="${custom.sname}"></font><br/>
用户密码:<input type="text" name="passwords"/><font color="red" th:errors="${custom.passwords}"></font><br/>
用户年龄:<input type="text" name="age"/><font color="red" th:errors="${custom.age}"></font><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>
</html>