ITeye博客

SpringBoot表单验证

andrew7676 · 2019-02-15 09:36 · SpringBoot · 编程语言 · Markdown

[size=medium][b]1. SpringBoot表单验证@Valid[/b][/size] [code="java"] 新建项目SpringDataValid src/main/resources/application.properties server.port=8000 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=root spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true src/main/java/com/andrew/entity/Student.java package com.andrew.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.Min; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; @Entity @Table(name="t_student") public class Student { @Id @GeneratedValue private Integer id; @NotEmpty(message="姓名不能为空!") @Column(length=50) private String name; @NotNull(message="年龄不能为空!") @Min(value=18,message="年龄必须大于18岁") private Integer age; 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; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } } src/main/java/com/andrew/dao/StudentDao.java package com.andrew.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.andrew.entity.Student; public interface StudentDao extends JpaRepository { } src/main/java/com/andrew/service/StudentService.java package com.andrew.service; import com.andrew.entity.Student; public interface StudentService { public void add(Student student); } src/main/java/com/andrew/service/impl/StudentServiceImpl.java package com.andrew.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.andrew.dao.StudentDao; import com.andrew.entity.Student; import com.andrew.service.StudentService; @Service("studentService") public class StudentServiceImpl implements StudentService { @Resource private StudentDao studentDao; @Override public void add(Student student) { studentDao.save(student); } } src/main/java/com/andrew/controller/StudentController.java package com.andrew.controller; import javax.annotation.Resource; import javax.validation.Valid; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.andrew.entity.Student; import com.andrew.service.StudentService; @RestController @RequestMapping("/student") public class StudentController { @Resource private StudentService studentService; @RequestMapping("/add") public String add(@Valid Student student,BindingResult bindingResult){ if (bindingResult.hasErrors()) { return bindingResult.getFieldError().getDefaultMessage(); } else { studentService.add(student); return "添加成功"; } } } src/main/webapp/jquery-1.4.4.js src/main/webapp/studentAdd.html 学生信息添加页面 姓名:
年龄:
SpringDataValidApplication.java右键Run As选择Spring Boot App http://localhost:8000/studentAdd.html 空 空 提交 年龄不能为空 空 1 提交 姓名不能为空 a 1 提交 年龄必须大于18岁 a 19 提交 添加成功 数据库t_student表新增记录 1 19 a [/code] [size=medium][b]2. SpringBoot表单验证注解清单[/b][/size] [table] |@Null|限制只能为null| |@NotNull|限制必须不为null| |@AssertFalse|限制必须为false| |@AssertTrue|限制必须为true| |@DecimalMax(value)|限制必须为一个不大于指定值的数字| |@DecimalMin(value)|限制必须为一个不小于指定值的数字| |@Digits(integer,fraction)|限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction| |@Future|限制必须是一个将来的日期| |@Max(value)|限制必须为一个不大于指定值的数字| |@Min(value)|限制必须为一个不小于指定值的数字| |@Past|限制必须是一个过去的日期| |@Pattern(value)|限制必须符合指定的正则表达式| |@Size(max,min)|限制字符长度必须在min到max之间| |@Past|验证注解的元素值(日期类型)比当前时间早| |@NotEmpty|验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)| |@NotBlank|验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格| |@Email|验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式| [/table]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。