Spring Hibernate自定义验证器来自属性文件的默认消息

时间:2013-09-22 15:21:07

标签: java spring hibernate spring-mvc

我有这个自定义的spring hibernate验证器和一个经过测试的message.properties文件(它可以工作)但是我无法正确设置这部分代码:{EmailExists.user.default} ...我的意思是,它没有得到属性文件中的EmailExists.user.default = blablabla var ...

package org.andreadorigo.webapp.validators.costraints;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

import org.andreadorigo.webapp.validators.EmailExistsValidator;

@Documented
@Constraint(validatedBy = EmailExistsValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface EmailExists {

    String message() default "EmailExists.user.default";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}

message_it.properties

constraints.dbexists=Il valore inserito è già presente nel database

hibernateContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:annotation-driven validator="validator" />
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />


    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:properties/messages</value>
                <value>classpath:properties/errors</value>
            </list>
        </property>
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="it" />
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>

    <mvc:interceptors>  
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
              <property name="paramName" value="lang"></property>
        </bean>
    </mvc:interceptors>



    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:properties/database.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
    </bean>




    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="messageInterpolator" ref="messageInterpolator"></property>
    </bean>
    <bean name="messageInterpolator"
        class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator">
        <constructor-arg ref="resourceBundleLocator" />
    </bean>
    <bean id="resourceBundleLocator"
        class="org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator">
        <constructor-arg ref="messageSource"></constructor-arg>
    </bean>





    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="org.andreadorigo.webapp.entities" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>

        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

2 个答案:

答案 0 :(得分:1)

如果违反约束,则返回用于创建错误消息的默认密钥的属性message应定义为:

String message() default "{EmailExists.user.default}";

以及message_it.properties文件中的相应条目:

EmailExists.user.default=Il valore inserito è già presente nel database

您需要注册用于验证@Controller方法的验证器:

    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="messageInterpolator" ref="messageInterpolator"></property>
    </bean>
    <bean name="messageInterpolator"
        class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator">
        <constructor-arg ref="resourceBundleLocator" />
    </bean>
    <bean id="resourceBundleLocator"
        class="org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator">
        <constructor-arg ref="messageSource"></constructor-arg>
    </bean>

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

答案 1 :(得分:0)

我想你也可以使用它:

<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
    <property name="basename" value="/messages/validation"/>
</bean>

您需要确保类路径中存在/messages/validation.properties文件。如果您使用的是Maven,可以将此文件放在resources文件夹(project/src/resources/messages/validation.properties)中。

请注意@Valid消息默认仅支持win-cp1251(ANSI)编码。