使用Hibernate验证器的Spring消息插值参数替换 [英] Spring Message Interpolation Argument Replacement using Hibernate Validator

查看:210
本文介绍了使用Hibernate验证器的Spring消息插值参数替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://开头docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-message-interpolation.html



据到休眠验证文档,可以在{min}和{max}参数相应地在错误消息中替换参数来完成以下操作。

  @Size (
min = 2,
max = 14,
message =车牌必须在{min}和{max}个字符之间长

我在Spring应用程序中使用这种表示法,但这些参数未被替换。
我回来了牌照必须在最小和最大字符之间

我需要配置验证程序以使其有效吗? p>

以下是我的配置:

 < bean id =messageSource类= org.springframework.context.support.ReloadableResourceBundleMessageSource > 
< property name =basenames>
< list>
<值>讯息< /值>
< / list>
< / property>
< property name =defaultEncodingvalue =UTF-8/>
< property name =useCodeAsDefaultMessagevalue =true/>
< / bean>

< bean id =validatorclass =org.springframework.validation.beanvalidation.LocalValidatorFactoryBean>
< property name =validationMessageSourceref =messageSource/>
< / bean>


解决方案

我找到原因。它是由属性useCodeAsDefaultMessage引起的,我们不能将它设置为true,只是使用默认值false。看来如果它是真的,只需从属性文件中的basenames中获取消息,并且不要在类路径根路径或org / hibernate / validator中使用默认消息文件!

https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-message-interpolation.html

According to the hibernate validation documentation, the following can be done where {min} and {max} parameters are replaced in error message accordingly.

 @Size(
            min = 2,
            max = 14,
            message = "The license plate must be between {min} and {max} characters long"
    )

I am using this notation in a Spring application but these arguments are not replaced. I get back "The license plate must be between min and max characters long"

Do I need to configure the validator differently to make this work?

Here is my configuration:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/> 
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>

   <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>

解决方案

I find the reason. It's caused by the property "useCodeAsDefaultMessage", we can't set it to "true",just use default value "false". It seems that if it is true, just get the messages from the properties file in the "basenames", and don't use the default message file in the classpath root path or "org/hibernate/validator"!

这篇关于使用Hibernate验证器的Spring消息插值参数替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆