第一节:SpringBoot 之表单验证@Valid
是spring-data-jpa的功能;
 
下面是添加学生的信息例子,要求姓名不能为空,年龄大于18岁。
 
贴下代码吧:
Student实体:
package com.cy.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;
}
}

StudentDao.java:

package com.cy.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.cy.entity.Student;

public interface StudentDao extends JpaRepository<Student, Integer>{

}

StudentService.java:

package com.cy.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cy.dao.StudentDao;
import com.cy.entity.Student; @Service
public class StudentService { @Resource
private StudentDao studentDao; public void add(Student student) {
studentDao.save(student);
}
}

StudentController.java:

package com.cy.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.cy.entity.Student;
import com.cy.service.StudentService; @RestController
@RequestMapping("/student")
public class StudentController { @Resource
private StudentService studentService; /**
* 添加学生
* @param student
* @return
*/
@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/studentAdd.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息添加页面</title>
<script src="resources/jquery/jquery.min.js"></script>
<script type="text/javascript"> function submitData(){
$.post("/student/add",
{
name:$("#name").val(),
age:$("#age").val()
},
function(result){
alert(result);
}
);
} </script>
</head>
<body>
姓名:<input type="text" id="name"/><br/>
年龄:<input type="text" id="age"/><br/>
<input type="button" onclick="submitData()" value="提交"/>
</body>
</html>

浏览器http://localhost/studentAdd.html,输入值不对,会提示:

补充验证的其他注解,这里只用了两个注解,下面列下清单,平时可以参考用;

限制                      说明
@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格式

最新文章

  1. Android UI体验之全屏沉浸式透明状态栏效果
  2. 介绍一些chrome 好用的插件和快捷键
  3. HDU 5213 Lucky 莫队+容斥
  4. 关于登录的会话控制, 终极解决方案 - chunyu
  5. Create a SharePoint Application Page for Anonymous Access
  6. Linux上程序执行的入口--Main
  7. android 63 Fragment
  8. Linux永久挂载远程网络目录
  9. SharePoint2010添加webpart找不到内容编辑器
  10. 2.12. 后端 SQL 的可见性(Core Data 应用程序实践指南)
  11. Tomcat服务器的Web安全的解决方法
  12. 配置用户Log on as service
  13. AI-2048 注释
  14. 分治FFT的三种含义
  15. RFS--RequestLibrary
  16. 超出JavaScript安全整数限制的数字计算-BigInt
  17. The MathType Dll cannot be found 问题解决办法
  18. break、continue区别
  19. Day9作业及默写
  20. Docker搭建Mysql容器

热门文章

  1. 为什么要使用encodeURL转换URL编码?
  2. 关于apicloud ios自定义模块引用第三方framework not found for architecture armv7
  3. button确定取消事件
  4. uri 定义
  5. JSP--TOMCAT-MYSQL web页面删除
  6. iOS开发之旅:实现一个APP界面框架
  7. 搭建一个SVN
  8. Apache和iis的冲突处理
  9. OpenCV 图像旋转实现
  10. Tensorflow 解决MNIST问题的重构程序