[英]spring mvc validation messages
我在弄清楚如何將驗證屬性從spring傳遞到外部化消息時遇到問題。 我正在使用Spring 4,並且已經包含“ validation-api-1.1.0.Final”和“ hibernate-validator-5.2.1.Final”。
我的模特:
@Size(min=20, max=64)
private String email;
當我返回表單時,仍然收到消息“ 電子郵件不應為空TCH {2} {1} {0} ”。
我的validationMessages.properties中包含以下行:
我還嘗試使用{min}和{max}以及%1和%0 。 這些都不起作用。
我的spring的xml配置具有以下配置:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:content/ValidationMessages</value>
<value>classpath:content/Language</value>
...
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
我還包括了spring-context-4.1.5.RELEASE庫。
任何想法 ??
請注意,我使用百里香葉渲染視圖。
提前謝謝。
基本上,您有一個錯誤消息模板,您想用給定約束的值填充該模板。
我認為您可以使用自定義ConstraintValidor來實現。
請參閱此處的Hibernate參考,尤其是第3.1.2.1節: https : //docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-customconstraints.html
我希望它能給我一些想法!
嘗試添加:
<mvc:annotation-driven validator="validator"/>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
例:
Size.user.name=Invalid username
並且請注意, user
中的user
是Controller
中ModelAttribute
值( @ModelAttribute("user")
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.