实体:

@Entity
public class User implements Serializable { /**
* 编号
*/
@Id
@GeneratedValue
private Long id; /**
* 名称
*/
@NotEmpty(message = "姓名不能为空")
@Size(min = 2, max = 8, message = "姓名长度必须大于 2 且小于 20 字")
private String name; /**
* 年龄
*/
@NotNull(message = "年龄不能为空")
@Min(value = 0, message = "年龄大于 0")
@Max(value = 300, message = "年龄不大于 300")
private Integer age; /**
* 出生时间
*/
@NotEmpty(message = "出生时间不能为空")
private String birthday;   ....//省略 }

html

<form th:action="@{/users/{action}(action=${action})}" method="post" class="form-horizontal">

                <input type="hidden" name="id" th:value="${user.id}"/>

                <div class="form-group">
<label for="user_name" class="col-sm-2 control-label">名称:</label>
<div class="col-xs-4">
<input type="text" class="form-control" id="user_name" name="name" th:value="${user.name}" th:field="*{user.name}" />
</div>
<label class="col-sm-2 control-label text-danger" th:if="${#fields.hasErrors('user.name')}" th:errors="*{user.name}">姓名有误!</label>
</div> <div class="form-group">
<label for="user_age" class="col-sm-2 control-label">年龄:</label>
<div class="col-xs-4">
<input type="text" class="form-control" id="user_age" name="age" th:value="${user.age}" th:field="*{user.age}" />
</div>
<label class="col-sm-2 control-label text-danger" th:if="${#fields.hasErrors('user.age')}" th:errors="*{user.age}">年龄有误!</label>
</div> <div class="form-group">
<label for="user_birthday" class="col-sm-2 control-label">出生日期:</label>
<div class="col-xs-4">
<input type="date" class="form-control" id="user_birthday" name="birthday" th:value="${user.birthday}" th:field="*{user.birthday}"/>
</div>
<label class="col-sm-2 control-label text-danger" th:if="${#fields.hasErrors('user.birthday')}" th:errors="*{user.birthday}">生日有误!</label>
</div> <div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-primary" type="submit" value="提交"/>&nbsp;&nbsp;
<input class="btn" type="button" value="返回" onclick="history.back()"/>
</div>
</div>
</form>

控制器方法

    @RequestMapping(value = "/create", method = RequestMethod.POST)
public String postUser(ModelMap map,
@ModelAttribute @Valid User user,
BindingResult bindingResult) { if (bindingResult.hasErrors()) {
map.addAttribute("action", "create");
return "userForm";
} userService.insertByUser(user); return "redirect:/users/";
}

效果图:

springboot 表单校验的更多相关文章

  1. 利用jquery.validate以及bootstrap的tooltip开发气泡式的表单校验组件

    表单校验是页面开发中非常常见的一类需求,相信每个前端开发人员都有这方面的经验.网上有很多成熟的表单校验框架,虽然按照它们默认的设计,用起来没有多大的问题,但是在实际工作中,表单校验有可能有比较复杂的个 ...

  2. AngularJS 1.2.x 学习笔记(表单校验篇)

    https://my.oschina.net/cokolin/blog/526911 摘要: 本文首发于 blog.csdn.net/vipshop_ebs/article/details/39472 ...

  3. angularJs表单校验(超级详细!!!)

    html代码 <!DOCTYPE html> <html ng-app="angularFormCheckModule"> <head> < ...

  4. bootstrap+jQuery.validate表单校验

    谈谈表单校验 这大概是一种惯例,学习前台后台最开始接触的业务都是用户注册和登录.现在社会坚持以人为本的理念,在网站开发过程同样如此.User是我们面对较多的对象,也是较核心的对象.最开始的用户注册和登 ...

  5. 关于jQuery表单校验的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. php 表单校验函数库(判断email格式是否正确、http地址是否合法有效、手机号码是否合法)

    /** * 表单校验函数库 */ /** * 判断email格式是否正确 * @param $email */ function is_email($email) { return strlen($e ...

  7. 关于jQuery表单校验

    <style> .red{border: 1px solid red;} .wrong-tip{color: red;} </style> <form action=&q ...

  8. jQuery.validate表单校验+bootstrap

    谈谈表单校验 这大概是一种惯例,学习前台后台最开始接触的业务都是用户注册和登录.现在社会坚持以人为本的理念,在网站开发过程同样如此.User是我们面对较多的对象,也是较核心的对象.最开始的用户注册和登 ...

  9. 【JAVAWEB学习笔记】28_jqueryAjax:json数据结构、jquery的ajax操作和表单校验插件

    Ajax-jqueryAjax 今天内容: 1.json数据结构(重点) 2.jquery的ajax操作(重点) 3.jquery的插件使用   一.json数据结构 1.什么是json JSON(J ...

随机推荐

  1. [转] 使用moment.js轻松管理日期和时间

    当前时间:moment().format('YYYY-MM-DD HH:mm:ss'); 2017-03-01 16:30:12 今天是星期几:moment().format('d'); 3 Unix ...

  2. Solr7.4.0的API(Solrj)操作

    一.SolrJ的概念 solr单机版服务搭建:https://www.cnblogs.com/frankdeng/p/9615253.html solr集群版服务搭建:https://www.cnbl ...

  3. Oracle数据库游标,序列,存储过程,存储函数,触发器

    游标的概念:     游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理, ...

  4. 循序渐进学.Net Core Web Api开发系列【15】:应用安全

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍W ...

  5. excel导入及注意事项

    在Excel导入过程中经常遇到单元格数据格式不正确引发的错误,在赋值前需要做下类型转换. 提供一个类型转换工具类: /** * 单元格类型转化工具类 * @param cell * @return * ...

  6. BZOJ4556 HEOI2016字符串

    没错,又是这题,使用后缀自动机,反向建树,主席树维护right集合. By:大奕哥 #include<bits/stdc++.h> using namespace std; ; ]; ch ...

  7. gdb 调试及优化

    调试程序时,在gdb内p var,会提示 No symbol "var" in current context. 即使没有使用任何编译优化选项,仍然不能查看,可能是这些变量被优化到 ...

  8. .net 中的async,await理解

    理解: 1.async修饰的方法可理解为异步方法(必须要配合await,否则和普通方法无异)2.当async方法执行遇到await,则立即将控制权转移到async方法的调用者3.由调用者决定是否需要等 ...

  9. BZOJ1712 : [Usaco2007 China]Summing Sums 加密

    设$s[i]$为进行$i$次加密后所有奶牛数字的和,有$s[i]=(n-1)s[i-1]$. 设$c[i]$为某头固定的奶牛进行$i$次加密后的数字, 若$i$为奇数,有: \[c[i]=((1-n) ...

  10. 【bzoj 1076】【SCOI2008】奖励关

    1076: [SCOI2008]奖励关 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 891[Submit][Status ...