【问题标题】:Spring - same custom message for multiple validation annotationsSpring - 多个验证注释的相同自定义消息
【发布时间】:2018-06-08 17:31:15
【问题描述】:

是否可以在 Spring 4+ 框架中为多个注释使用相同的自定义消息。

就我而言,我想为 @NotNull 和 @NotEmpty 注释显示相同的消息。

@NotEmpty(message = "First name cannot be empty.")
@NotNull(message = "First name cannot be empty.")

【问题讨论】:

  • 在您的应用中添加此内容时发生了什么?
  • 在这种情况下你不需要 NotNull,NotEmpty 涵盖了 null。

标签: java spring spring-mvc


【解决方案1】:

是的,这是可能的。消息不用作系统中的标识符,因此它是可能的。我建议使用ValidationMessages.properties 和

@NotEmpty(message = "{first.notnullorempty}")
@NotNull(message = "{first.notnullorempty}")

请参阅:https://stackoverflow.com/a/4811273/5585182,以防止一遍又一遍地复制粘贴相同的消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2015-02-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多