我不能“覆盖"带有.properties的@NotEmpty消息 [英] I can't "override" @NotEmpty message with .properties

查看:89
本文介绍了我不能“覆盖"带有.properties的@NotEmpty消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Spring 3,我正在尝试使用类org.hibernate.validator.constraints.NotEmpty来验证表单.

I'm studying Spring 3 and im trying to validate a form using the class org.hibernate.validator.constraints.NotEmpty.

例如,可以覆盖@Size消息(javax.validation.constraints.Size)和@Email消息(org.hibernate.validator.constraints.Email),以从.properties文件中检索消息.

Im able, for instance, to override the @Size message (javax.validation.constraints.Size) and the @Email message (org.hibernate.validator.constraints.Email) retrieving the messages from a .properties file.

但是我无法覆盖org.hibernate.validator.constraints.NotEmpty默认消息.我总是收到默认消息可能不为空"

But im not able to override the org.hibernate.validator.constraints.NotEmpty default message. I always get the default message "may not be empty"

这是我的XXXX-servlet.xml

this is my XXXX-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 <mvc:annotation-driven/>     

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

    <context:component-scan base-package="com.springgestioneerrori.controller" />     

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>    

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8"/>
    </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="en"/>
    </bean>

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

</beans>

这是我的User类

package com.springgestioneerrori.model;    


import javax.validation.constraints.Size;   
import org.hibernate.validator.constraints.NotEmpty;

public class Utente {


    @NotEmpty 
    @Size(min=3,max=20)
    private String nome;            

    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }   

}

这是我的表格

.......
<form:form action="formSent" method="post" commandName="utente">
<form:errors path="*" cssClass="errorblock" element="div" /><br/><br/>
<spring:message code="form.label.utente.nome"/><form:input path="nome"/><form:errors path="nome" cssClass="error" /><br>  
<input type="submit">
</form:form>
 ......

这是我的控制者

........
    @RequestMapping("/formSent")
    public String formSent(Model model, @Valid Utente utente, BindingResult result){    

        if(result.hasErrors()){
            model.addAttribute("utente", utente);   
            return "form";
        }
        else{
            model.addAttribute("msg", "inserimento effettuato con successo");
            return "formSent";
        }       
    }
.........

这是我的财产文件

    NotEmpy.utente.nome = il campo nome non può essere vuoto //Im NOT able to get this message
    Size.utente.nome = Il nome deve contenere tra 2 e 30 lettere //Im able to get this message

推荐答案

您必须创建文件ValidationMessages.properties并将其放置在应用程序的类路径中. Hibernate验证程序使用org.hibernate.validator.constraints.NotEmpty.message键来翻译消息

You have to create a file ValidationMessages.properties and place it in the classpath of your application. The Hibernate validator use the org.hibernate.validator.constraints.NotEmpty.message key to translate the message

org.hibernate.validator.constraints.NotEmpty.message=il campo nome non può essere vuoto

在这里查看更多信息:

http://docs.jboss.org/hibernate/validator/4.3/reference/zh-CN/html/validator-usingvalidator.html#section-message-interpolation

更新

要自定义单个消息,请使用以下内容:

For customizing a single message use something like this:

@NotEmpty( message = "{NotEmpy.utente.nome}" )
@Size(min=3,max=20, message = "{Size.utente.nome}")
private String nome;

这篇关于我不能“覆盖"带有.properties的@NotEmpty消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆