2

我有这个自定义的 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>
4

2 回答 2

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" />
于 2013-09-23T10:18:48.377 回答
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) 编码。

于 2015-10-08T22:08:33.853 回答