Why Spring Message Interpolation Argument Replacement using Hibernate Validator don't work?

可紊 提交于 2019-12-24 10:44:41

问题


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.


回答1:


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"!



来源:https://stackoverflow.com/questions/29416136/why-spring-message-interpolation-argument-replacement-using-hibernate-validator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!