2014-09-19 2 views
1

스프링 3을 공부 중이고 org.hibernate.validator.constraints.NotEmpty 클래스를 사용하여 양식의 유효성을 검사하려고합니다..properties로 @NotEmpty 메시지를 "재정의"할 수 없습니다.

임 수, 예를 들어, @Size 메시지 (javax.validation.constraints.Size)과 .properties 파일에서 메시지를 검색 @Email 메시지 (org.hibernate.validator.constraints.Email)를 오버라이드 (override)합니다.

그러나 org.hibernate.validator.constraints.NotEmpty 기본 메시지를 무시할 수 없습니다. 나는 항상 기본 메시지

"비어있을 수 없습니다"얻을이이 내 사용자 클래스

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; 
    } 

} 

내 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> 

입니다 이것은 내 양식

입니다
....... 
<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> 
...... 

이것은 내 조절기입니다.

,210
........ 
    @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"; 
     }  
    } 
......... 

이 당신은 파일 ValidationMessages.properties을 만들고 응용 프로그램의 클래스 경로에 배치해야 내 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

입니다.

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

UPDATE

같은 하나의 메시지 사용 무언가를 사용자 정의 : 최대 절전 모드 검사기는

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

여기에 좀 더 정보를 가지고 메시지를 변환하는 org.hibernate.validator.constraints.NotEmpty.message 키를 사용하여 이 :

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

도와 줘서 고마워. 나는 매우 어리석은 실수를 저질렀다. 속성 파일에서 NotEmpty 대신 NotEmpy를 썼습니다. waisting 당신을 위해 죄송합니다 :) – MDP

0

이 시도 -

@NotEmpty (메시지 = "{NotEmpy.utente.nome}") 개인 문자열 놈;

+0

도와 주셔서 감사합니다. 나는 매우 어리석은 실수를 저질렀다. 속성 파일에서 NotEmpty 대신 NotEmpy를 썼습니다. waisting 당신을 위해 죄송합니다 :) – MDP

0

도움 주셔서 감사합니다. 나는 매우 어리석은 실수를 저질렀다. 속성 파일에서 NotEmpty 대신 NotEmpy를 썼습니다. waisting 당신을 위해 죄송합니다 :)

관련 문제