springmvc如何集成JSR-303进行数据验证在之前的如下文章中已经介绍过了:
SpringMVC数据验证——第七章 注解式控制器的数据验证、类型转换及格式化——跟着开涛学SpringMVC
举个例子:
比如我的验证
@Length(min = 5, max = 200, message = "{message.title.length.not.valid}") @Column(name = "title") private String title;
有朋友想得到min、max及此时的title值,可以在消息文件中通过:
当然也可以使用{value} 获取此时的title值
这到底是怎么工作的呢?
在JSR-303中,使用javax.validation.MessageInterpolator来解析消息,而如果:
<!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册--> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties --> <property name="validationMessageSource" ref="messageSource"/> </bean>
即此时使用的hibernate实现,注入了spring的messageSource来解析消息时:
public void setValidationMessageSource(MessageSource messageSource) { this.messageInterpolator = HibernateValidatorDelegate.buildMessageInterpolator(messageSource); }
/** * Inner class to avoid a hard-coded Hibernate Validator 4.1+ dependency. */ private static class HibernateValidatorDelegate { public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) { return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource)); } }
即内部委托给了org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator#ResourceBundleMessageInterpolator:
并使用如下代码解析消息:
public String interpolate(String message, Context context) { // probably no need for caching, but it could be done by parameters since the map // is immutable and uniquely built per Validation definition, the comparison has to be based on == and not equals though return interpolateMessage( message, context.getConstraintDescriptor().getAttributes(), defaultLocale ); }
此处可以看到context.getConstraintDescriptor().getAttributes(),其作用是获取到注解如@Length上的所有数据,具体代码实现如下:
private Map<String, Object> buildAnnotationParameterMap(Annotation annotation) { final Method[] declaredMethods = ReflectionHelper.getDeclaredMethods( annotation.annotationType() ); Map<String, Object> parameters = new HashMap<String, Object>( declaredMethods.length ); for ( Method m : declaredMethods ) { try { parameters.put( m.getName(), m.invoke( annotation ) ); } catch ( IllegalAccessException e ) { throw log.getUnableToReadAnnotationAttributesException( annotation.getClass(), e ); } catch ( InvocationTargetException e ) { throw log.getUnableToReadAnnotationAttributesException( annotation.getClass(), e ); } } return Collections.unmodifiableMap( parameters ); }
循环每一个方法 并获取值放入map,接着进入方法:
private String interpolateMessage(String message, Map<String, Object> annotationParameters, Locale locale)
具体实现思路如下:
1、首先查询缓存中是否存在,如果存在直接获取缓存中解析的消息:
if ( cacheMessages ) { resolvedMessage = resolvedMessages.get( localisedMessage ); }
2、如果没有,按照JSR-303规定的使用三步获取:
首先委托给ResourceBundle获取消息值:
ResourceBundle userResourceBundle = userResourceBundleLocator .getResourceBundle( locale ); ResourceBundle defaultResourceBundle = defaultResourceBundleLocator .getResourceBundle( locale );
2.1、委托给用户定义的resourceBundle进行解析(即我们之前指定的messageSource),递归的查找消息并替换那些转义的:
// search the user bundle recursive (step1) userBundleResolvedMessage = replaceVariables( resolvedMessage, userResourceBundle, locale, true );
转义的包括:
\\{、\\}、\\\\。
所谓递归的查找意思就是如:
a=hello {b}
b=123
会在解析a时再递归解析b,如果{b}就是一个字符串,而不想被解析,可以通过\\{b\\}转移完成;
替换完转义字符后,还是会再递归的查找下去。
2.2、使用默认的resourceBundle(即默认找org.hibernate.validator.ValidationMessages.properties)按照和2.1一样的步骤执行:
// search the default bundle non recursive (step2) resolvedMessage = replaceVariables( userBundleResolvedMessage, defaultResourceBundle, locale, false ); evaluatedDefaultBundleOnce = true;
2.3、解析完成后,接着替换注解变量值:
// resolve annotation attributes (step 4) resolvedMessage = replaceAnnotationAttributes( resolvedMessage, annotationParameters ); // last but not least we have to take care of escaped literals resolvedMessage = resolvedMessage.replace( "\\{", "{" ); resolvedMessage = resolvedMessage.replace( "\\}", "}" ); resolvedMessage = resolvedMessage.replace( "\\\\", "\\" ); return resolvedMessage;
如之前说的
@Length(min = 5, max = 200, message = "{message.title.length.not.valid}")
消息:
标题长度必须在{min}到{max}个字符之间
那么,如果没有在之前的resourceBundle中得到替换,那么会被注解的值替换掉。
即得到标题长度必须在5到200个字符之间。
此处有一个小问题:
如果你的messageSource添加了:
<property name="useCodeAsDefaultMessage" value="true"/>
意思就是如果找不到key对应的消息,则使用code作为默认消息;这样会引发一个问题就是,根据code找消息,永远能找到,即不可能成功执行【2.3】。
如“标题长度必须在{min}到{max}个字符之间”,如果消息文件中没有min 和 max,实际得到的是:
”标题长度必须在min到max个字符之间“,不是我们期望的;
如“标题长度必须在\\{min\\}到max个字符之间”,实际也会获取到:
”标题长度必须在min到max个字符之间“,也不是我们期望的。
所以实际使用时useCodeAsDefaultMessage应该为false。
相关推荐
redis-service_pubsub.rar。 springMVC集成spring-data-redis实现发布订阅。
hibernate-validator-5.2.4.Final.jar是对上述接口的实现;或者在Maven中添加依赖:1.<dependency> 2. <groupId>org.hibernate</groupId> 3. <artifactId>hibernate-validator</artifactId> 4. <version>5.2.4.Final...
SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- Hello...
本篇文章主要介绍了SpringMVC 使用JSR-303进行校验 @Valid示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
SpringMVC-Mybatis-Shiro-redis-master..............
本篇文章主要介绍了详解springmvc使用JSR-303进行数据校验,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
基于SpringMVC Mybatis Shiro Redis 的权限管理系统,该系统已经部署到线上,线上访问地址:http://shiro.itboy.net,登录账号:admin 密码:sojson,,详细教程参考sojson.com/shiro
基于SpringMVC、Mybatis、Redis、Freemarker的Shiro管理Demo源码的升级版 相关详细配置说明请参看 --> http://www.sojson.com/shiro
项目中所需的jar包在压缩包中已提供,项目中用到了JSR303和hibernate-validator的技术,大量采用了注解@NotNull,@Email,@Length,@Max,@Pattern,@Size等,此外还采用了自定义注解,验证信息全部配置在属性文件中.
SpringMVC精品资源--深入解析SpringMVC核心原理:从手写简易版MVC框架开始(SmartMvc)
hibernate-validator-5.2.4.Final.jar hibernate-validator-annotation-processor-5.2.4.Final.jar hibernate-validator-cdi-5.2.4.Final.jar validation-api-1.1.0.Final.jar jboss-logging-3.2.1.Final.jar ...
SpringMVC------从HelloWorld开始
NULL 博文链接:https://lpyyn.iteye.com/blog/2268837
用户Spring mvc Jackson自动转化的支持
springmvc-framework-官方中文版
springmvc-json-lib.rar springmvc-json-lib.rar
SpringMVC精品资源--基于springMVC实现的解决方案系统
easyui的tree组件的使用,使用springmvc作为后台支持,前后通信是实现tree的操作
SpringMVC精品资源--单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传
springmvc-without-webxml springmvc-without-webxmlspringmvc-without-webxml