SpringBoot使用Hibernate Validator表单验证
【摘要】 @[toc] 一、要求用户名必须输入,并且长度范围为5~20年龄范围为18~60工作日期在系统时间之前 二、实现步骤 2.1 新建实体类package com.ch.practice5_1.model;import java.util.Date;import javax.validation.constraints.NotBlank;import javax.validation.const...
@[toc]
一、要求
- 用户名必须输入,并且长度范围为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输入如下数据,点击添加
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)