在 Hibernate 中使用自定义验证消息春天

发布于 2025-01-04 23:35:51 字数 1127 浏览 2 评论 0原文

我尝试了此处答案中的步骤: Hibernate Validator、自定义 ResourceBundleLocator 和 Spring

但仍然只是将 {location.title.notEmpty} 作为输出而不是消息。

dispatcher-servlet.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>/WEB-INF/validationMessages</value>
        </list>
    </property>
</bean>

/WEB-INF/validationMessages.properties:

location.title.notEmpty=My custom message

表单(类位置)

@NotEmpty( message = "{location.title.notEmpty}" )
private String title;

这里出了什么问题?

I tried the steps from the answer here: Hibernate Validator, custom ResourceBundleLocator and Spring

But still just getting {location.title.notEmpty} as output instead of the message.

dispatcher-servlet.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>/WEB-INF/validationMessages</value>
        </list>
    </property>
</bean>

/WEB-INF/validationMessages.properties:

location.title.notEmpty=My custom message

Form (Class Location)

@NotEmpty( message = "{location.title.notEmpty}" )
private String title;

What's going wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晨说晚安 2025-01-11 23:35:51

知道了! :-)

我将以下 bean 添加到了我的 dispatcher-servlet.xml 中,而不是上面提到的两个:

  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/validationMessages" />
  </bean>

然后,在我的validationMessages.properties中,我使用了以下语法:

NotEmpty.location.title=My custom Message

我想这就是原因:我使用了Hibernate 验证注释(不是 javax 的)

并且一般来说,消息文件的语法应该类似于

[ConstraintName].[ClassName].[FieldName]=[Message]

希望这可以帮助其他人;-)

要从 spring 控制器访问消息,只需添加 @Autowired private消息源messageSource; 作为类字段并使用 messageSource.getMessage 方法。因为我只使用一种语言环境,所以我使用了 messageSource.getMessage( "NotEmpty.location.title", null, null )

Got it! :-)

I added the following bean instead the above mentioned two into my dispatcher-servlet.xml:

  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/validationMessages" />
  </bean>

Then, in my validationMessages.properties I used the following syntax:

NotEmpty.location.title=My custom Message

I guess this is the reason: I used the Hibernate validation annotations (not the javax ones)

And for general the syntax of the messages file should look like

[ConstraintName].[ClassName].[FieldName]=[Message]

Hope this helps some other people out there ;-)

And to access the message from a spring controller just add @Autowired private MessageSource messageSource; as a class field and use the messageSource.getMessage methods. Because I'm just using one locale I used messageSource.getMessage( "NotEmpty.location.title", null, null )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文