springboot 表单校验
2018-02-01 16:22
357 查看
实体:
html
控制器方法
效果图:
@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="提交"/> <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 @valid 表单校验
- spring boot获取表单文件踩的坑
- Spring boot 使用 shiro做安全校验
- SpringBoot开发详解(五)--Controller接收参数以及参数校验
- spring参数校验,form表单形式,以及http接口形式
- spring boot项目添加拦截器验证token防止表单重复提交
- SpringBoot下实现前端验证码图片的生成和校验
- 【SpringBoot】Spring Boot进阶之Web进阶( 第1章 课程介绍 第2章 Web进阶-表单验证 )
- Springboot学习参数校验(五)-1
- Spring Boot 整合Thymeleaf 之表单提交 问题
- SpringBoot 结合 JSR303 对前端数据进行校验
- SpringBoot之表单验证@Valid
- spring boot基础之表单验证
- SpringBoot非官方教程 | 第二十篇: 处理表单提交
- SpringBoot页面表单请求失败,错误400的解决办法
- Springboot 使用 JSR 303 对 Controller 控制层校验及 Service 服务层 AOP 校验 使用消息资源文件对消息国际化
- spring boot validator校验
- spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- SpringBoot-Hibernate校验
- Spring boot 表单提交失败Validation failed for object='book'. Error count: 2