2014-09-19 62 views
1

我正在學習Spring 3,並試圖使用類org.hibernate.validator.constraints.NotEmpty驗證表單。我不能用.properties覆蓋@NotEmpty消息

例如,我可以覆蓋@Size消息(javax.validation.constraints.Size)和@Email消息(org.hibernate.validator.constraints.Email)從.properties文件中檢索消息。

但我不能重寫org.hibernate.validator.constraints.NotEmpty默認消息。我總是得到默認消息 「不能爲空」

這是我的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"; 
     }  
    } 
......... 

這是我的propertis文件

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 

回答

3

你必須創建一個文件ValidationMessages.properties並將其放置在你的應用程序的classpath。 Hibernate的驗證使用關鍵信息

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

看一看這裏瞭解更多信息翻譯:

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

UPDATE

有關自定義一個消息使用類似這個:

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

感謝您的幫助。我犯了一個非常愚蠢的錯誤。在屬性文件內我寫NotEmpy而不是NotEmpty。對不起,懷着你的時間:) – MDP 2014-09-22 06:59:35

0

嘗試此 -

@NotEmpty(消息= 「{NotEmpy.utente.nome}」) 私人字符串諾姆;

+0

感謝您的幫助。我犯了一個非常愚蠢的錯誤。在屬性文件內我寫NotEmpy而不是NotEmpty。對不起,要你時間:) – MDP 2014-09-22 06:59:10

0

感謝您的幫助。我犯了一個非常愚蠢的錯誤。在屬性文件內我寫NotEmpy而不是NotEmpty。對不起,懷着你的時間:)