Spring MVC i18n Hibernate验证

如何根据我的语言正确地获得弹簧消息验证,我得到的是默认的hibernate验证器,而不是注释@NotEmpt中定义的消息.

    @Entity
    public class News {

        @Id
        private Integer idNews;

        @NotEmpty(message = "{news.title.error}")
        @Column
        private String title;
              ........
    }

在我的配置中:

    
        
    

<!-- locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" >
    <property name="defaultLocale" value="pt" />
</bean>

<!-- Access resource bundles with the specified basename -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>/WEB-INF/i18n/messages</value>
            <value>/WEB-INF/i18n/validation</value>
        </list>
    </property>
</bean>

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

最佳答案 你的问题也许可以像这个问题一样解决:

https://jira.springsource.org/browse/SPR-8658?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

重点是

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

希望这可以帮到你

点赞