开发者社区> mr_伍先生> 正文

springboot 表单校验

简介: 实体: @Entity public class User implements Serializable { /** * 编号 */ @Id @GeneratedValue private Long id; /** * 名...
+关注继续查看

实体:

@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】表单验证和使用AOP处理请求
【SpringBoot】表单验证和使用AOP处理请求
98 0
springboot统一表单数据校验
统一表单数据校验 在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动 方法内代码显得冗长 每次要看哪些参数验证是否完整,需要去翻阅验证逻辑代码 hibernate validator(官方文档)提供了一套比较完善、便捷的验证实现方式。
1753 0
传统maven项目和现在spring boot项目的区别
Spring Boot:传统 Web 项目与采用 Spring Boot 项目区别
112 0
创建springboot项目的基本流程——以宠物类别为例
创建springboot项目的基本流程——以宠物类别为例
45 0
SpringBoot 项目与被开发快速迁移|学习笔记
快速学习 SpringBoot 项目与被开发快速迁移
55 0
SpringSecurity与SpringBoot在集中式项目中整合步骤说明|学习笔记
快速学习SpringSecurity与SpringBoot在集中式项目中整合步骤说明
56 0
自定义SpringBoot项目的启动Banner
``Banner``是``SpringBoot``框架一个特色的部分,其设计的目的无非就是一个框架的标识,其中包含了版本号、框架名称等内容,既然``SpringBoot``为我们提供了这个模块,它肯定也是可以更换的这也是``Spring``开源框架的设计理念。
125 0
基于springboot+mybatisplus+vue-科技项目评审及专家库管理系统
基于springboot+mybatisplus+vue-科技项目评审及专家库管理系统
64 0
【Java】【Spring Boot】CP01:创建一个SpringBoot项目(Spring Initializr)
【Java】【Spring Boot】CP01:创建一个SpringBoot项目(Spring Initializr)
52 0
使用aliyunECS服务器+宝塔面板部署springboot后端项目并测试接口
在部署过程中遇到了很多问题,解决起来也是十分繁琐,这里写个笔记记录一下遇到的问题和思路 这里我先打算测试以下后端接口,前端代码还没有进行运行。 还没有购买域名,因为域名需要备案时间要一周所以暂时使用公网ip进行访问。
173 0
+关注
mr_伍先生
java,编程语言,算法兴趣
文章
问答
文章排行榜
最热
最新
相关电子书
更多
使用Spring.Initializr定制工程脚手架
立即下载
陈曦:使用Spring.Initializr定制工程脚手架
立即下载
Spring框架入门
立即下载