1
我用Spring4 + Hibernate5 Validator來做驗證。爲什麼使用Hibernate Validator的Spring Message Interpolation參數替換不起作用?
我不想使用默認ValidateMessage.properties,所以我在classpath根目錄下的國際化目錄下定義自己的消息屬性, 的配置信息如下:
<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"/>
Bean驗證信息是:
@Size(
min = 2,
max = 14,
message = "The license plate must be between {min} and {max} characters long"
)
我在Spring應用程序中使用這種表示法,但這些參數不被替換。我回來了「牌照必須在最小和最大字符之間」。
指Spring Message Interpolation Argument Replacement using Hibernate Validator
我不知道爲什麼它不工作,任何人都可以幫助解決這個問題?謝謝。
同意,檢查該方法的API,推薦用於開發。默認情況下是false,所以你可以刪除該行。 – 2015-09-18 16:47:27