Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为spring中抛出的ny@Valid注释错误消息创建属性文件_Java_Spring_Jsp - Fatal编程技术网

Java 如何为spring中抛出的ny@Valid注释错误消息创建属性文件

Java 如何为spring中抛出的ny@Valid注释错误消息创建属性文件,java,spring,jsp,Java,Spring,Jsp,我有一个类,其中字段“nome”不能为空 public class Utente { ...... @NotEmpty @Size(min=2, max=30) private String nome; ....... } 在用户填写并发送表单后,我调用此方法 @RequestMapping("/formSent") public String formSentMethod(Model model, @Valid

我有一个类,其中字段“nome”不能为空

public class Utente {       
    ......

    @NotEmpty 
    @Size(min=2, max=30)
    private String nome;
    .......    
}
在用户填写并发送表单后,我调用此方法

    @RequestMapping("/formSent")
    public String formSentMethod(Model model, @Valid Utente utente, BindingResult result){
    .....
}   
我在Eclipse的控制台中用这段普通代码打印错误,代码放在formSentMethod中

    @RequestMapping("/formSent")
    public String formSentMethod(Model model, @Valid Utente utente, BindingResult result){
    .....
    for(ObjectError errore : result.getAllErrors()){            
        System.out.println(errore.getDefaultMessage());         
    }
    .....
}   
到目前为止,当用户没有填写字段“nome”时,我得到了默认的错误消息“maybotempty”

我试图通过使用一个名为messages.properties的属性文件来定制该消息,我将该文件放在WEB-INF/classes下,如图所示

在我写的messages.properties中

   NotEmpty.utente.nome = Il nome è obbligatorio
在我的XXX-servlet.xml中,我以这种方式“调用”属性文件

<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">   

            <property name="basename" value="messages" />
    </bean> 

<mvc:annotation-driven />
<property name="basename" value="WEB-INF/Classes/messages" />

因为它不起作用,我甚至试着用这种方式编辑basename值

<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">   

            <property name="basename" value="messages" />
    </bean> 

<mvc:annotation-driven />
<property name="basename" value="WEB-INF/Classes/messages" />

但效果不太好


我不断地得到默认的错误消息,而不是我定制的消息“Il nomeèobbligatorio”。可能我在属性文件中写的内容不正确。我做错了什么?

我认为消息应该放在
类中,但根据您的配置,应该向上一级

您还可以创建一个绝对路径,如:

/WEB-INF/类/消息

还是再上一层

/WEB-INF/信息


另外,请记住,使用此配置时,您没有设置默认编码,这可能会导致一些问题。我建议将属性
defaultEncoding
设置为
UTF-8
message。属性不引用类和字段,而是引用表单名称和字段。例如,在本html中:

<form:form method="POST" commandName="utente" action="customer/signup">
    <form:errors path="*" cssClass="errorblock" element="div" />
    <table>
        <tr>
            <td>Customer Name :</td>
            <td><form:input path="nome" /></td>
            <td><form:errors path="nome" cssClass="error" /></td>
        </tr>
        <tr>
            <td>Customer Age :</td>
            <td><form:input path="age" /></td>
            <td><form:errors path="age" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" /></td>
        </tr>
    </table>
</form:form>
MessageSource
实例插入控制器中

@Autowired
private MessageSource messageSource;
要获得您的信息,请执行以下操作

for (Object object : bindingResult.getAllErrors()) {
    if(object instanceof FieldError) {
        FieldError fieldError = (FieldError) object;

        /**
          * Use null as second parameter if you do not use i18n (internationalization)
          */

        String message = messageSource.getMessage(fieldError, null);
    }
}

你能检查一下你的.war文件中是否存在messages.properties文件吗?你能显示显示错误的视图/html部分吗?@dieend我编辑了我的文章。到目前为止,我在Eclipse的控制台上打印了错误消息。我的意思是检查message.properties文件是否存在于war文件中。无需更改messageSource的值。看起来它无法加载messages.properties文件。t尝试在控制器中@Autowire消息源,并检查它是否包含键/值对。