在 Hibernate 中使用自定义验证消息春天
我尝试了此处答案中的步骤: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了! :-)
我将以下 bean 添加到了我的
dispatcher-servlet.xml
中,而不是上面提到的两个:然后,在我的validationMessages.properties中,我使用了以下语法:
我想这就是原因:我使用了Hibernate 验证注释(不是 javax 的)
并且一般来说,消息文件的语法应该类似于
希望这可以帮助其他人;-)
要从 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
:Then, in my validationMessages.properties I used the following syntax:
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
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 themessageSource.getMessage
methods. Because I'm just using one locale I usedmessageSource.getMessage( "NotEmpty.location.title", null, null )