本文介绍了为什么使用Hibernate Validator的Spring Message Interpolation Argument替换不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想使用默认的ValidateMessage.properties,所以我在自定义的消息属性下定义了自己的消息属性。

我使用Spring4 + Hibernate5 Validator进行验证。在classpath根目录下的I18N目录中,
的配置信息如下:

 < bean id =messageSource类= org.springframework.context.support.ResourceBundleMessageSource > 
< property name =basenamesvalue =I18N.messages,I18N.validation.ValidationMessages/>
< property name =defaultEncodingvalue =UTF-8/>
用于开发和调试。
默认为false.-->
< property name =useCodeAsDefaultMessagevalue =true/>
,因此如果他没有找到语言环境,则使用message.properties。 - >
< property name =fallbackToSystemLocalevalue =false/>
< / bean>

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

< mvc:annotation-driven validator =validator/>

bean验证信息是:

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

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



参考



我不知道为什么它不起作用,任何人都可以帮助解决它?谢谢。

解决方案

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


I used Spring4 + Hibernate5 Validator to do the validation.

I don't want use the default ValidateMessage.properties, so I define my own message properties under the I18N directory in the classpath root directory,the config information is below:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="I18N.messages,I18N.validation.ValidationMessages" />
    <property name="defaultEncoding" value="UTF-8" />
    <!-- Set whether to use the message code as default message instead of throwing a NoSuchMessageException.
        Useful for development and debugging.
        Default is "false".-->
    <property name="useCodeAsDefaultMessage" value="true"/>
    <!-- The key here is fallbackToSystemLocale which prevents the system to look into the system,
        thus using "message.properties" if he doesn't find the locale. -->
    <property name="fallbackToSystemLocale" value="false"/>
</bean>

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

<mvc:annotation-driven validator="validator"/>

the bean validation info is :

 @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".

refer to Spring Message Interpolation Argument Replacement using Hibernate Validator

I don't know why it doesn't work, anyone could help to resolve it? Thanks.

解决方案

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 Validator的Spring Message Interpolation Argument替换不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:07