SpringBoot做数据校验Validate(数据效验,对页面中输入的数据进行校验)
在开发项目时,肯定会对数据做一些校验,比如:登录用户时,需要校验用户名的长度,可用性。密码的长度,或者是年龄,指定为0到100,有些人可能比较长寿,那就0到150岁。不过目前过百的年龄应该也不怎么会使用电脑吧。这就不讨论了。关于这个数据校验也没有太多可以说的。直接上代码演示就好。
页面:ok.html
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>操作成功</title>
</head>
<body>
OK。。。。
</body>
</html>
页面add.html
<!DOCTYPE html>
<html 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="${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="text" nam