SpringBoot服务端数据校验

目录

1 SpringBoot 对表单做数据校验

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

1.2 SpringBoot 表单数据校验步骤

1.2.1 在实体类中添加校验规则

1.2.2 在 Controller 中开启校验

1.2.3 在页面中获取提示信息

1.2.4 遇到异常

1.3 解决数据校验时的异常问题

1.3.1 controller层需要改写

1.3.2 如果参数的名称需要做改变

1.4 其他校验规则


1 SpringBoot 对表单做数据校验

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

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

1.2 SpringBoot 表单数据校验步骤

1.2.1 在实体类中添加校验规则

public class Users {
    @NotBlank //非空校验
    private String name;
    @NotBlank(message="自定义校验信息") //非空校验
    private String password;
    private Integer age;
//省去get/set方法
}

1.2.2 在 Controller 中开启校验

注意@Valid是开启对前台传过来的对象进行校验;

BindingResult:是封装了校验的结果,把校验的结果按照对象的驼峰命名方式放到Model对象里面,因此前台要按照驼峰命名的形式取出

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

1.2.3 在页面中获取提示信息

<!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><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>

1.2.4 遇到异常

1.3 解决数据校验时的异常问题

解决异常的方法, 在跳转页面的方法中注入一个对象, 来解决问题。 要求参数对象的变量名必须是对象的类名的全称首字母小写

注意: 由于springmvc 会将该对象放入到 Model 中传递。 key 的名称会使用该对象的驼峰式的命名规则来作为 key。参数的变量名需要与对象的名称相同,将首字母小写。

1.3.1 controller层需要改写

/**
* 解决异常的方式。 可以在跳转页面的方法中注入一个 Uesrs 对象。
* 注意: 由于 springmvc 会将该对象放入到 Model 中传递。 key 的名称会使用
该对象的驼峰式的命名规则来作为 key。
* 参数的变量名需要与对象的名称相同。 将首字母小写。
* *
@param users
* @return
*/
@RequestMapping("/addUser")
public String showPage( Users users){
return "add";
}
/**
* 完成用户添加
*@Valid 开启对 Users 对象的数据校验
*BindingResult:封装了校验的结果
*/
@RequestMapping("/save")
public String saveUser( @Valid Users users,BindingResult result){
if(result.hasErrors()){
return "add";
}
 System.out.println(users);
return "ok";
}

1.3.2 如果参数的名称需要做改变

如果想为传递的对象更改名称, 可以使用@ModelAttribute("aa")这表示当前传递的对象的 key 为 aa。那么我们在页面中获取该对象的 key 也需要修改为 aa

/**
* *
如果想为传递的对象更改名称, 可以使用@ModelAttribute("aa")这表示当
前传递的对象的 key 为 aa。
* 那么我们在页面中获取该对象的 key 也需要修改为 aa
* @param users
* @return
*/
@RequestMapping("/addUser")
public String showPage(@ModelAttribute("aa") Users users){
return "add";
}
/**
* 完成用户添加
*@Valid 开启对 Users 对象的数据校验
*BindingResult:封装了校验的结果
*/
@RequestMapping("/save")
public String saveUser(@ModelAttribute("aa") @Valid Users
users,BindingResult result){
if(result.hasErrors()){
return "add";
} S
ystem.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="${aa.name}"></font><br/>
用户密码: <input type="password" name="password" /><font
color="red" th:errors="${aa.password}"></font><br/>
用户年龄: <input type="text" name="age" /><font color="red"
th:errors="${aa.age}"></font><br/>
<input type="submit" value="OK"/>
</form>
</body>
</html>

1.4 其他校验规则

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

猜你喜欢

转载自blog.csdn.net/u012060033/article/details/89512625