【问题标题】:Hibernate Custom Validator Messages Spring MVCHibernate 自定义验证器消息 Spring MVC
【发布时间】:2019-04-21 16:01:15
【问题描述】:

我正在尝试为 Hibernate 验证注释(NOTNULL、NOTEMPTY 等)自定义消息,以便应用程序能够从 .proparties 文件中读取它们。

我在这里尝试了答案中的解决方案:use custom validation in hibernate hibernate validator

但对我来说没有任何效果我仍然收到默认消息(可能不是 null ,可能不是空的,....)。

代码示例:

应用程序上下文.xml:

<bean name="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="resourceBundleLocator" />
    </property>
</bean>

<bean name="resourceBundleLocator"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>messages</value>
        </list>
    </property>
</bean>

user.java:

@NotNull
private String name;

消息.属性:

user.name.notNull = incorrect name

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    您需要在 @NotNull 注释中提及键名。

    @NotNull(meassage="{user.name.notNull}")
    private String name;
    

    您可以通过链接了解更多信息 https://teamtreehouse.com/library/displaying-validation-messages

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2012-03-03
      • 1970-01-01
      • 2016-10-09
      • 2016-06-10
      • 1970-01-01
      相关资源
      最近更新 更多