SpringBoot使用Hibernate Validator表单验证
文章目录
一、要求
- 用户名必须输入,并且长度范围为5~20
- 年龄范围为18~60
- 工作日期在系统时间之前
二、实现步骤
2.1 新建实体类
package com.ch.practice5_1.model;
import java.util.Date;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Past;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
public class MyUser {
@NotBlank(message="用户名必须输入")
@Length(min=5, max=20, message="用户名长度在5到20之间")
private String uname;
@Range(min=18,max=60,message="年龄在18到60之间")
private Integer age;
@DateTimeFormat(pattern="yyyy-MM-dd")
@Past(message="工作日期在系统时间之前")
private Date workdate;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getWorkdate() {
return workdate;
}
public void setWorkdate(Date workdate) {
this.workdate = workdate;
}
}
2.2、创建控制器类UserController
package com.ch.practice5_1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ch.practice5_1.model.MyUser;
@Controller
public class UserController {
@RequestMapping("/")
public String input(@ModelAttribute("myUser") MyUser mu) {
//@ModelAttribute("myUser")与th:object="${myUser}"相对应
return "input";
}
@RequestMapping("/add")
public String add( @ModelAttribute("myUser") @Validated MyUser mu,BindingResult rs) {
//@ModelAttribute("myUser")与th:object="${myUser}"相对应
if(rs.hasErrors()){//验证失败
return "input";
}
//验证成功
return "success";
}
}
2.3、在templates目录下加入input.html和success.html
input.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<!-- 默认访问 src/main/resources/static下的css文件夹-->
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">数据验证</h3>
</div>
</div>
<div class="container">
<div>
<h4>添加用户</h4>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<form class="form-horizontal" th:action="@{/add}" th:object="${myUser}" method="post">
<div class="form-group">
<div class="input-group col-md-6">
<span class="input-group-addon">
<i class="glyphicon glyphicon-pencil">用户名</i>
</span>
<input class="form-control" type="text"
th:field="*{uname}" th:placeholder="请输入用户名"/>
</div><span th:errors="*{uname}"></span>
</div>
<div class="form-group">
<div class="input-group col-md-6">
<span class="input-group-addon">
<i class="glyphicon glyphicon-pencil">年龄</i>
</span>
<input class="form-control" type="text"
th:field="*{age}" th:placeholder="请输入年龄"/>
</div><span th:errors="*{age}"></span>
</div>
<div class="form-group">
<div class="input-group col-md-6">
<span class="input-group-addon">
<i class="glyphicon glyphicon-time">工作日期</i>
</span>
<input class="form-control" type="date"
th:field="*{workdate}" th:placeholder="请选择工作日期"/>
</div> <span th:errors="*{workdate}"></span>
</div>
<div class="form-group">
<div class="col-md-6">
<div class="btn-group btn-group-justified">
<div class="btn-group">
<button type="submit" class="btn btn-success">
<span class="glyphicon glyphicon-share"></span>
添加
</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
success.html
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
添加成功!
</body>
</html>
2.4、修改application.properties文件
server.servlet.context-path=/practice5_1
2.5 启动Practice51Application主类,然后访问http://localhost:8080/practice5_1/
2.6输入如下数据,点击添加
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?