Spring Boot学习——表单验证
我觉得表单验证主要是用来防范小白搞乱网站和一些低级的黑客技术。Spring Boot可以使用注解 @Valid 进行表单验证。下面是一个例子。
例子说明:数据库增加一条Student记录,要求学生年龄限制在12——20岁之间。
首先,定义一个Student类,代码如下:
package *; //自己定义 import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min; @Entity
public class Student {
@Id
@GeneratedValue
private Integer id;
private String name;
@Min(value = 12,message = "学生年龄最小不能低于12岁")
@Max(value = 20, message = "学生年龄最大不能高于20岁")
private Integer age; public Student(){
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
使用@Min和@Max注解,@Min表示最小值,@Max表示最大值,message是说明。
其次,定义数据库操作接口StudentRepository,代码如下:
package *; //自己定义 import com.aston.reader.model.Student;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface StudentRepository extends JpaRepository<Student, Integer>{
}
最后,使用注解 @Valid 表单验证
package *; //自己定义
import *.StudentRepository;
import *.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
public class StudentController {
@Autowired
private StudentRepository studentRepository;
/**
* 添加学生
* @return
*/
@PostMapping(value = "/addStudent")
public Student addStudent(@Valid Student student, BindingResult bindingResult){
if (bindingResult.hasErrors()){//表单验证,报错
System.out.println(bindingResult.getFieldError().getDefaultMessage());
return null;
}
student.setAge( student.getAge());
student.setName( student.getName());
return studentRepository.save( student);
}
}
Spring Boot学习——表单验证的更多相关文章
- spring+thymeleaf实现表单验证数据双向绑定
前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...
- 学习 表单验证插件validate
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- spring boot学习(7) SpringBoot 之表单验证
第一节:SpringBoot 之表单验证@Valid 是spring-data-jpa的功能: 下面是添加学生的信息例子,要求姓名不能为空,年龄大于18岁. 贴下代码吧: Student实体: ...
- 玩转spring boot——AOP与表单验证
AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...
- Spring Boot 2 + Thymeleaf:服务器端表单验证
表单验证分为前端验证和服务器端验证.服务器端验证方面,Java提供了主要用于数据验证的JSR 303规范,而Hibernate Validator实现了JSR 303规范.项目依赖加入spring-b ...
- Spring Boot 表单验证、AOP统一处理请求日志、单元测试
一.使用@Valid表单验证 于实体类中添加@Min等注解 @Entity public class Girl { @Id @GeneratedValue private Integer id; pr ...
- Spring Boot笔记八:表单验证
所谓的表单验证,就是为了防止用户乱输入的,这个问题前端的HTML5就可以判断了,其实不需要后端来验证,这里还是讲一下后端验证 首先,我们的Person类,我们加上一些表单验证的注释,如下: packa ...
- Spring进行表单验证
转自:https://www.tianmaying.com/tutorial/spring-form-validation 开发环境 IDE+Java环境(JDK 1.7或以上版本) Maven 3. ...
- jQuery学习之路(8)- 表单验证插件-Validation
▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...
随机推荐
- hprose 1.0(rpc 框架) - 关于跨域和P3P的声明
private function sendHeader($context) { if ($this->onSendHeader !== null) { $sendHeader = $this-& ...
- 826. Most Profit Assigning Work
https://leetcode.com/problems/most-profit-assigning-work/description/ class Solution { public: int m ...
- Gender Equality in the Workplace【职场上的性别平等】
Gender Equality in the Workplace A new batch of young women - members of the so-called Millennial ge ...
- 【树状数组】CF961E Tufurama
挺巧妙的数据结构题(不过据说这是一种套路? E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode ...
- A1083 List Grades (25)(25 分)
A1083 List Grades (25)(25 分) Given a list of N student records with name, ID and grade. You are supp ...
- 51nod_1199 树的先跟遍历+区间更新树状数组
题目是中文,所以不讲题意 做法顺序如下: 使用先跟遍历,把整棵树平铺到一维平面中 使用自己整的区间更新树状数组模板进行相关操作. http://www.cnblogs.com/rikka/p/7359 ...
- Winform VS2015打包
首先 ,我们要去官网http://learn.flexerasoftware.com/content/IS-EVAL-InstallShield-Limited-Edition-Visual-Stud ...
- input框中的必填项之取消当前input框为必填项
html5新增了一个required属性,可以使用这个属性对文本框设置必填项,直接在input文本框上添加required即可 . 效果如图:
- 做一个日收入100元的APP!
[导语]虽然讲了很多个人开发者的文章,但新手开发者如何赚自己的第一个100块钱,确是最难的事情.群里有人说都不知道干什么 app赚钱,完全没有想法, 并且经常问我有什么快速赚钱的方法.我只能遗憾地说, ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...