标签:ppi bean temp hold seconds rop 兼容 ref 字符串
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.4</version> </dependency>
注意:
<mvc:annotation-driven conversion-service="conversionService" validator="validator"/> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property> <property name="validationMessageSource" ref="messageSource"></property> </bean> <!-- 检验错误信息资源配置文件 --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:validator/validationMessages</value> </list> </property> <!-- 资源文件编码格式 --> <property name="fileEncodings" value="utf-8"/> <!-- 对资源文件内容的缓存时间,单位秒 --> <property name="cacheSeconds" value="120"/> </bean>
private Integer stuId; @NotBlank(message = "{notnull}") @Size(max = 20, min = 2, message = "姓名长度在2-20之间") private String name; @Max(value = 200, message = "最大年龄200岁") @Min(value = 6, message = "最小年龄6岁") private Integer age;
注意:
message=“{notnull}”会去配置的错误文件中查找,即validator/validationMessages.properties文件中查找notnull的配置对应的错误信息。
a) 自动校验
在实体上增加@Validated注解,表示要校验这个实体。BindingResult用于接收验证后的错误信息。
@RequestMapping(value = "/add", method = { RequestMethod.GET, RequestMethod.POST }) public String addStudent(Model model, HttpServletRequest request, @Validated Student stu, BindingResult bindingResult) { if (HttpRequestContext.GET.equalsIgnoreCase(request.getMethod())) { return "prictice/student/addStudent"; } else { if (bindingResult.hasErrors()) { List<ObjectError> errors = bindingResult.getAllErrors(); model.addAttribute("errors", errors); return "prictice/student/addStudent"; } stu.setRegTime(new Date()); stu.setStatus(true); studentService.addStudent(stu); return "redirect:/student/list"; } }
b) 主动校验
通过工具类,主动验证某个实体类
新建工具类ValidateUtil.java
public class ValidateUtil { public static <T> void validate(T obj) { LocalValidatorFactoryBean validator = (LocalValidatorFactoryBean) SpringContextHolder.getBean("validator"); Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj); Iterator<ConstraintViolation<T>> iter = constraintViolations.iterator(); while (iter.hasNext()) { ConstraintViolation<T> error = iter.next(); StringBuffer buffer = new StringBuffer().append("[") .append(error.getPropertyPath().toString()).append("]") .append(error.getMessage()); throw new IllegalArgumentException(buffer.toString()); } } }
或者可以使用下面的代码获取validator
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
调用
@RequestMapping(value = "/add") public String addStudent(@RequestParam Student stu) { ValidateUtil.validate(stu); return "redirect:/student/list" }
注解 |
作用 |
AssertTrue |
布尔值为真 |
AssertFalse |
布尔值为假 |
Null |
引用为空 |
NotNull |
引用不为空 |
NotEmpty |
字符串引用和值都不是空 |
Min |
数字的最小值 |
Max |
数字的最大值 |
Past |
日期必须是过去 |
Future |
日期必须是未来 |
Pattern |
字符串必须匹配正则表达式 |
Valid |
递归验证引用 |
Size |
验证字符串是否在Size范围内 |
|
验证字符串是否是一个有效的电子邮箱 |
URL |
字符串是否是一个有效的URL |
注意:
标签:ppi bean temp hold seconds rop 兼容 ref 字符串
原文地址:https://www.cnblogs.com/lpob/p/10891511.html