Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring 带批注的表单验证-转换自定义消息失败_Spring_Spring Mvc - Fatal编程技术网

Spring 带批注的表单验证-转换自定义消息失败

Spring 带批注的表单验证-转换自定义消息失败,spring,spring-mvc,Spring,Spring Mvc,我正在分析spring mvc showcase示例项目()。当我点击不正确的日期格式(屏幕截图上的出生日期字段)时,我对JSP页面上显示验证响应的方式感到困惑。如果没有那些ConversionFailedException详细信息,我如何通过一些自定义消息使其更加用户友好 截图: 应用注释驱动的验证。下面是bean类中表示生日字段的代码段 FormBean.java @DateTimeFormat(iso=ISO.DATE) @Past private Date birthDate; 负责

我正在分析spring mvc showcase示例项目()。当我点击不正确的日期格式(屏幕截图上的出生日期字段)时,我对JSP页面上显示验证响应的方式感到困惑。如果没有那些ConversionFailedException详细信息,我如何通过一些自定义消息使其更加用户友好

截图:

应用注释驱动的验证。下面是bean类中表示生日字段的代码段

FormBean.java

@DateTimeFormat(iso=ISO.DATE)
@Past
private Date birthDate;
负责表格提交的方法:

FormController.java

@RequestMapping(method=RequestMethod.POST)
public String processSubmit(@Valid FormBean formBean, BindingResult result, 
                            @ModelAttribute("ajaxRequest") boolean ajaxRequest, 
                            Model model, RedirectAttributes redirectAttrs) {
    if (result.hasErrors()) {
        return null;
    }
    // Typically you would save to a db and clear the "form" attribute from the session 
    // via SessionStatus.setCompleted(). For the demo we leave it in the session.
    String message = "Form submitted successfully.  Bound " + formBean;
    // Success response handling
    if (ajaxRequest) {
        // prepare model for rendering success message in this request
        model.addAttribute("message", message);
        return null;
    } else {
        // store a success message for rendering on the next request after redirect
        // redirect back to the form to render the success message along with newly bound values
        redirectAttrs.addFlashAttribute("message", message);
        return "redirect:/form";            
    }
}

请注意,您正在处理这里的绑定错误。在执行实际的JSR-303验证之前很久就会抛出这些命令,它们会覆盖失败字段的JSR-303约束冲突

绑定错误的代码是
类型不匹配
。例如,您可以将以下内容添加到消息属性中:

typeMismatch.birthDate = Invalid birth date format.
检查JavaDoc以了解Spring的错误代码解析是如何工作的。

是否使用了错误标记? 您可以在验证批注中使用消息属性。 就像这里:

@NotEmpty(message=“serverIP不能为空”)
私有字符串服务器IP

请显示您的视图。这是公共github示例,我在帖子的开头粘贴了链接。下面是exact view form.jsp:在我的servlet-context.xml中,我在messages.properties中添加了以下内容:
,正如您建议的那样:
类型不匹配。生日
没有发生任何事情您一定是做错了什么。我自己刚刚克隆了这个项目,添加了消息源+消息,它就工作了。你的
messages.properties
文件在哪里?你说得对。我无意中把它放在src/main/webapp文件夹中。现在我把它移到src/main/resources中,它就像一个符咒。非常感谢。此错误不是来自验证注释,而是来自Spring DataBinder,因此对验证注释的更改不会影响其消息。