spring - 如何显示 Spring MVC 表单错误的类错误

标签 spring validation spring-mvc

我熟悉使用 Spring 的 <form:errors>标签来显示对象属性的验证错误,但是如何在类级别显示错误?

以下是我所讨论内容的示例:

@ScriptAssert(lang = "javascript", 
        script = "_this.isExistingEmployee == true || (_this.phoneNumber != null && _this.address != null)", 
        message = "New hires must enter contact information")
public class JobApplicationForm {

    @NotBlank(message = "First Name is required")
    private String firstName;

    @NotBlank(message = "Last Name is required")
    private String lastName;

    @NotNull(message = "Please specify whether you are an existing employee in another area")
    private Boolean isExistingEmployee;

    private String phoneNumber;

    private String address;
}

@ScriptAssert这里只是确认,如果申请人表明他们是现有员工,他们可以跳过联系信息,但如果不是,他们必须输入它。当此验证失败时,错误不在给定字段上,而是在类上。

在表单中,我可以使用 <form:errors path="firstName"> 在字段上显示错误我可以使用 <form:errors path="*"> 立即显示所有错误(类和字段)但如何隔离和显示类级别错误?

最佳答案

要仅显示类级别错误,请将路径留空。

<form:errors path="">

来自 Spring 文档:

  • path="*" - displays all errors
  • path="lastName" - displays all errors associated with the lastName field
  • if path is omitted - object errors only are displayed

引用:http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/view.html#view-jsp-formtaglib-errorstag

关于spring - 如何显示 Spring MVC 表单错误的类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17925358/

相关文章:

spring - spring mvc 中的 Servlet

java - 如何在 Spring Boot 应用程序中测试 @Configuration 和 @Bean?

使用注释和全局拒绝的 Spring Security 白名单方法

java - Spring Integration 从 REST 服务获取分页结果

java - FilterRegistrationBean 需要使用 Spring Security 启用 CORS 支持吗?

javascript - 如何在文本框中的客户端验证 youtube url

java - Spring Boot安全: java. lang.StackOverflowError

JSF 2.0 中的 AJAX onSubmit 验证

javascript - Jquery Validation Unvalidated form submit button can be clicked (With Fiddle)

java - 如何对MySQL进行动态查询