gpt4 book ai didi

java - 我不能使用 .properties "override"@NotEmpty 消息

转载 作者:行者123 更新时间:2023-11-29 05:17:46 26 4
gpt4 key购买 nike

我正在学习 Spring 3,我正在尝试使用 org.hibernate.validator.constraints.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>

这是我的用户类

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

这是我的 Controller

........
@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 validator 使用 org.hibernate.validator.constraints.NotEmpty.message 键来翻译消息

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

更新

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

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

关于java - 我不能使用 .properties "override"@NotEmpty 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25934781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com