5

我熟悉使用 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="*">但是如何隔离和显示类级别的错误?

4

1 回答 1

14

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

<form:errors path="">

来自 Spring 文档:

  • path="*" - 显示所有错误
  • path="lastName" - 显示与 lastName 字段相关的所有错误
  • 如果省略路径 - 仅显示对象错误

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

于 2013-07-29T14:09:58.297 回答