SpringBoot-数据校验

1.SpringBoot 对表单数据校验的技术特点

1.1SpringBoot 中使用了 Hibernate-validate (现在已经被javax.validation.constraints数据校验取代)校验框架(springboot中默认已经集成,无需手动引入约束)

2.SpringBoot 表单数据校验步骤

2.1在实体类中添加校验规则

package com.example.demo.pojo;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

import org.hibernate.validator.constraints.Length;

public class Custom {
	@Length(max=5,min=2,message = "为2-5个字符!")
	@NotBlank(message = "用户名不能为空!请重新填写!")
	private String sname;
	@NotBlank(message = "密码为空!!!")
	private String passwords;
	@Max(value = 100,message = "年龄最大为100")
	@Min(value = 18,message = "年龄最小为18")
	private Integer age;
	public Custom() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Custom(String sname, String passwords, Integer age) {
		super();
		this.sname = sname;
		this.passwords = passwords;
		this.age = age;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getPasswords() {
		return passwords;
	}
	public void setPasswords(String passwords) {
		this.passwords = passwords;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Custom [sname=" + sname + ", passwords=" + passwords + ", age=" + age + "]";
	}
	

}

校验注解:

@NotBlank判断字符串是否为 null 或者是空串(去掉首尾空格)
@NotEmpty判断字符串是否 null 或者是空串
@Length判断字符的长度(最大或者最小)
@Min判断数值最小值
@Max判断数值最大值
@Email判断邮箱是否合法

2.2在contorller中开启校验

package com.example.demo.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.RequestMapping;


import com.example.demo.pojo.Custom;
import com.example.demo.service.CustomService;

@Controller
public class CustomController {
	@Autowired
	private CustomService customService;

	@RequestMapping("/addCustom")
/**
	 * 
	 *需要在跳转页面的方法中注入一个 Custom 对象,否则将出现异常。
	 *注意:由于 SpringMVC 会将该对象放入到 Model 中传递。key 的名称会使用
	 *该对象的驼峰式的命名规则来作为 key。这就要求参数的变量名需要与对象的名称相同。
	 *并且要求参数对象的变量名必须是对象的类名的全称首字母小写。
	 */
	public String showPage(Custom custom) {
		return "insert";
	};
	@RequestMapping("/inserts")
/**
	 *
	 * @Valid 开启对 Users 对象的数据校验
	 * BindingResult:封装了校验的结果
	 */
	public String insertCustom(@Valid Custom custom,BindingResult result) {
		if(result.hasErrors()) {
			return "insert";		
			}else {
			customService.insertCustom(custom);
			return "ok";
		}
		
	}

}

2.3在页面中开启信息提示

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

	<form th:action="@{/inserts}" method="post">
		用户姓名:<input type="text" name="sname"/><font color="red" th:errors="${custom.sname}"></font><br/>
		用户密码:<input type="text" name="passwords"/><font color="red" th:errors="${custom.passwords}"></font><br/>
		用户年龄:<input type="text" name="age"/><font color="red" th:errors="${custom.age}"></font><br/>
		<input type="submit" value="确定"/><br/>
	</form>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

城南皮卡丘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值