当前位置: 代码迷 >> VC/MFC >> Spring mvc 调整hibernate-validator
  详细解决方案

Spring mvc 调整hibernate-validator

热度:187   发布时间:2016-05-02 03:23:53.0
Spring mvc 整合hibernate-validator
一、MAVEN配置
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.2.Final</version>
        </dependency>
       
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
  
二、Spring MVC 配置
        <mvc:annotation-driven validator="validator"/>

        <!-- 国际化配置 -->
        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">   
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>   
        <!-- 这里配置将使用上面国际化配置的messageSource --> 
        <property name="validationMessageSource" ref="messageSource"/>   
        </bean>  
       
        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <!-- 国际化信息所在的文件名 -->
        <property name="basenames"> 
            <list> 
                <value>classpath:bundle/messages</value> 
                <value>classpath:bundle/validation/ValidationMessages</value> 
            </list> 
        </property> 
        <!-- 文件编码:注意,实际文件也需要修改为UTF-8类型
                       这样就能以中文的方式书写 -->       
        <property name="defaultEncoding" value="UTF-8"/>
        </bean>

三、实际文件目录
resources/bundle/validation/ValidationMessages.properties
  相关解决方案