首页>代码>Apache Shiro+SpringMVC+Hibernate Search+Hibernate+Bootstrap企业信息管理系统基础框架搭建整合实例代码教程>/jeesite3/src/main/java/com/thinkgem/jeesite/common/beanvalidator/BeanValidators.java
package com.thinkgem.jeesite.common.beanvalidator; import java.util.List; import java.util.Map; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import javax.validation.Validator; import com.google.common.collect.Lists; import com.google.common.collect.Maps; /** * JSR303 Validator(Hibernate Validator)工具类. * * ConstraintViolation中包含propertyPath, message 和invalidValue等信息. * 提供了各种convert方法,适合不同的i18n需求: * 1. List<String>, String内容为message * 2. List<String>, String内容为propertyPath + separator + message * 3. Map<propertyPath, message> * * 详情见wiki: https://github.com/springside/springside4/wiki/HibernateValidator * @author calvin */ public class BeanValidators { /** * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void validateWithException(Validator validator, Object object, Class<?>... groups) throws ConstraintViolationException { Set constraintViolations = validator.validate(object, groups); if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } /** * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>中为List<message>. */ public static List<String> extractMessage(ConstraintViolationException e) { return extractMessage(e.getConstraintViolations()); } /** * 辅助方法, 转换Set<ConstraintViolation>为List<message> */ @SuppressWarnings("rawtypes") public static List<String> extractMessage(Set<? extends ConstraintViolation> constraintViolations) { List<String> errorMessages = Lists.newArrayList(); for (ConstraintViolation violation : constraintViolations) { errorMessages.add(violation.getMessage()); } return errorMessages; } /** * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为Map<property, message>. */ public static Map<String, String> extractPropertyAndMessage(ConstraintViolationException e) { return extractPropertyAndMessage(e.getConstraintViolations()); } /** * 辅助方法, 转换Set<ConstraintViolation>为Map<property, message>. */ @SuppressWarnings("rawtypes") public static Map<String, String> extractPropertyAndMessage(Set<? extends ConstraintViolation> constraintViolations) { Map<String, String> errorMessages = Maps.newHashMap(); for (ConstraintViolation violation : constraintViolations) { errorMessages.put(violation.getPropertyPath().toString(), violation.getMessage()); } return errorMessages; } /** * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath message>. */ public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e) { return extractPropertyAndMessageAsList(e.getConstraintViolations(), " "); } /** * 辅助方法, 转换Set<ConstraintViolations>为List<propertyPath message>. */ @SuppressWarnings("rawtypes") public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations) { return extractPropertyAndMessageAsList(constraintViolations, " "); } /** * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath +separator+ message>. */ public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) { return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator); } /** * 辅助方法, 转换Set<ConstraintViolation>为List<propertyPath +separator+ message>. */ @SuppressWarnings("rawtypes") public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations, String separator) { List<String> errorMessages = Lists.newArrayList(); for (ConstraintViolation violation : constraintViolations) { errorMessages.add(violation.getPropertyPath() + separator + violation.getMessage()); } return errorMessages; } }

heng356 LV1
2月11日
zshldehao1 LV2
2月5日
zwjxxyxqf LV1
2024年11月1日
colecole LV3
2024年10月8日
HTML-WEB LV1
2024年5月22日
educationAAA LV11
2024年5月3日
2860615178 LV4
2024年5月1日
managment LV3
2024年4月17日
vluobo LV1
2024年3月19日
formatself1 LV2
2024年1月28日

missliu LV8
前天
哪里的完整版 LV8
4月1日
GakkiMarryMe LV9
3月25日
akittyboy LV9
2月24日
heng356 LV1
2月11日
林kkkkkk LV6
2月10日
zshldehao1 LV2
2月5日
sureivv LV2
1月9日
微信网友_7290593889931264
2024年12月10日
暂无贡献等级
sjwc88 LV3
2024年12月4日