SpringBoot服务端数据校验

一) SpringBoot 对表单数据校验的技术特点

1.1SpringBoot 中使用了 Hibernate-validate 校验框架 2 SpringBoot 表单数据校验步骤
2.1在实体类中添加校验规则

在是实体类上使用的validate 框架提供的注解实现非空,长度,大小的简单校验。约束可以应用于任何访问类型的字段(公共,私有等)。但是,不支持对静态字段的约束。

1.@NotBlank 注解用于非空校验

@NotBlank(message = "用户名不许为空") //非空验证
private String username;
@NotBlank
private String password;

private Integer userAge;

2.控制器
@Valid 注解表示当前对象需要进行验证
BindingResult 对象封装了校验的结果

package com.sxt.controller;

import com.sxt.pojo.Users;
import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

/**
 * @project_name:hibernate02
 * @date:2019/8/24:14:53
 * @author:shinelon
 * @Describe:
 */
@Controller
public class UserController {


    @RequestMapping(value = "/add")
    public String toJumpUserAdd(Users users){
        return "add";
    }

    /**
     * @param null
     * @description:  @Valid 注解表示当前被需要被验证的对象,该对对象在页面传递的的时候被创建,如果出现验证失败,会被model对象
     *                 在次传递到页面上。
     *                 BindingResult:对象用于验证的的接口,会被自动注入值,拥有校验是否出错
     * @return:
     * @author: shinelon
     * @time: 2019/8/24:15:03
     */
    @RequestMapping(value = "/userAdd")
    public String userAdd(@Valid  Users users, BindingResult result){
        if(result.hasErrors()){//判断是否有验证错误
            return "add";
        }
        return "ok";
    }

}

页面

<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form th:action="@{/userAdd}" method="post">
    用户名:<input type="text" name="username" value=""/><font color="red" th:errors="${users.username}"></font><br/>
    密码:<input type="password" name="password" value="" /><font color="red" th:errors="${users.password}"></font><br/>
    年龄:<input type="text" name="userAge" value=""/><font color="red" th:errors="${users.userAge}"></font><br/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

校验的效果
在这里插入图片描述

遇到异常

在这里插入图片描述

解决数据校验时的异常问题
解决异常的方法,在跳转页面的方法中注入一个对象,来解决问题。要求参数对象的 变量名必须是对象的类名的全称首字母小写。
或者会用注解@Modelattribute注解指定的存在的model内的名字

@RequestMapping(value = "/add")
public String toJumpUserAdd(Users users){
    return "add";
}

异常产生的原因,

这个的thymeleaf模板标签的特性有关,在进行页面跳转的时候,对add.html进行解析的时候,th:error内获取作用域中的对象users,而此时的users并不存在,所以才会报错,
解决办法:1,在跳转的方法上加上参数Users,在进行页面的跳转的时候会自动的将Users对象加入到Model对象内

<form th:action="@{/userAdd}" method="post">
    用户名:<input type="text" name="username" value=""/><font color="red" th:errors="${users.username}"></font><br/>
    密码:<input type="password" name="password" value="" /><font color="red" th:errors="${users.password}"></font><br/>
    年龄:<input type="text" name="userAge" value=""/><font color="red" th:errors="${users.userAge}"></font><br/>
    <input type="submit" value="提交"/>
</form>

解决办法二:在前面加上一个的非空判断

<form th:action="@{/userAdd}" method="post">
    用户名:<input type="text" name="username" value=""/><font color="red" th:if="${users != null}" th:errors="${users.username}"></font><br/>
    密码:<input type="password" name="password" value="" /><font color="red" th:if="${users != null}" th:errors="${users.password}"></font><br/>
    年龄:<input type="text" name="userAge" value=""/><font color="red"  th:if="${users != null}" th:errors="${users.userAge}"></font><br/>
    <input type="submit" value="提交"/>
</form>

二)其他的注解

注解 说明
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Digits(integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(regex=, flag=) 被注释的元素必须符合指定的正则表达式
@NotBlank(message =) 验证字符串非null,且长度必须大于0
@Email 被注释的元素必须是电子邮箱地址
@Length(min=, max=) 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range(min=, max=, message=) 被注释的元素必须在合适的范围内
@NotBlank 自动祛除收尾空格
@NotEmpty 判断是否为空,不祛除首尾空格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值