加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何为类错误显示Spring MVC表单错误

发布时间:2020-12-15 01:37:12 所属栏目:大数据 来源:网络整理
导读:我熟悉使用Spring的 标签显示对象属性的验证错误,但是如何在类级别显示错误? 这是我正在谈论的一个例子: @ScriptAssert(lang = "javascript",script = "_this.isExistingEmployee == true || (_this.phoneNumber != null _this.address != null)",message

我熟悉使用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 =“*”>一次显示所有错误(类和字段)但是如何隔离并显示类级错误?

最佳答案
要仅显示类级错误,请将路径保留为空.

来自Spring文档:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读