博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Validation(使用Hibernate Validator)
阅读量:2118 次
发布时间:2019-04-30

本文共 3640 字,大约阅读时间需要 12 分钟。

1、需要的jar包

hibernate-validator.5.1.3.Final.jar

validation-api.1.1.0.Final.jar

2、springsevlet-config.xml配置

在spring3之后,任何支持JSR303的validator(如Hibernate Validator)都可以通过简单配置引入,只需要在配置xml中加入,这时validatemessage的属性文件默认为classpath下的ValidationMessages.properties

如果不使用默认,可以使用下面配置:

 3、hibernate validator constraint 注解

Bean Validation 中内置的 constraint         @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=)  被注释的元素必须符合指定的正则表达式        Hibernate Validator 附加的 constraint    @NotBlank(message =)   验证字符串非null,且长度必须大于0    @Email  被注释的元素必须是电子邮箱地址    @Length(min=,max=)  被注释的字符串的大小必须在指定的范围内    @NotEmpty   被注释的字符串的必须非空    @Range(min=,max=,message=)  被注释的元素必须在合适的范围内

 4、使用validator

在需要校验的对象前增加@Valid 注解(该注解位于javax.validation包中)来触发校验。这样就可以完成针对输入数据User对象的校验了,校验结果任然保存在BindingResult对象中。

1 package com.mkyong.common.model; 2   3 import org.hibernate.validator.constraints.NotEmpty; 4 import org.hibernate.validator.constraints.Range; 5   6 public class Customer { 7   8     @NotEmpty //make sure name is not empty 9     String name;10  11     @Range(min = 1, max = 150) //age need between 1 and 15012     int age;13  14     //getter and setter methods15  16 }
1 package com.mkyong.common.controller; 2   3 import javax.validation.Valid; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.ui.ModelMap; 6 import org.springframework.validation.BindingResult; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import com.mkyong.common.model.Customer;10  11 @Controller12 @RequestMapping("/customer")13 public class SignUpController {14  15     @RequestMapping(value = "/signup", method = RequestMethod.POST)16     public String addCustomer(@Valid Customer customer, BindingResult result) {17  18         if (result.hasErrors()) {19             return "SignUpForm";20         } else {21             return "Done";22         }23  24     }25  26     @RequestMapping(method = RequestMethod.GET)27     public String displayCustomerForm(ModelMap model) {28  29         model.addAttribute("customer", new Customer());30         return "SignUpForm";31  32     }33  34 }
1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 2  3  4 17 18  19 20     

Customer SignUp Form - JSR303 @Valid example

21 22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Customer Name :
Customer Age :
39
40 41 42

可以通过定义“validatemessage.properties”文件,覆盖定义在持久化对象上的错误提示,通常属性文件中属性key为“@Annotation Name.object.fieldname“的形式:

NotEmpty.customer.name = Name is required!Range.customer.age = Age value must be between 1 and 150

代码运行Demo:

5、参考

from: http://www.cnblogs.com/afeng7882999/p/4300032.html

你可能感兴趣的文章
Ubuntu解决gcc编译报错/usr/bin/ld: cannot find -lstdc++
查看>>
解决Ubuntu14.04 - 16.10版本 cheese摄像头灯亮却黑屏问题
查看>>
解决Ubuntu 64bit下使用交叉编译链提示error while loading shared libraries: libz.so.1
查看>>
VS生成DLL文件供第三方调用
查看>>
Android Studio color和font设置
查看>>
Python 格式化打印json数据(展开状态)
查看>>
Centos7 安装curl(openssl)和libxml2
查看>>
Centos7 离线安装RabbitMQ,并配置集群
查看>>
Centos7 or Other Linux RPM包查询下载
查看>>
运行springboot项目出现:Type javax.xml.bind.JAXBContext not present
查看>>
Java中多线程向mysql插入同一条数据冲突问题
查看>>
Idea Maven项目使用jar包,添加到本地库使用
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>