在Hibernate + Spring中使用自定义验证消息 [英] Use custom validation messages in Hibernate + Spring

查看:77
本文介绍了在Hibernate + Spring中使用自定义验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这里的答案: Hibernate验证器,自定义ResourceBundleLocator和Spring



但仍然只是获取 {location.title.notEmpty} 作为输出,而不是消息。 / p>

dispatcher-servlet.xml

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

< bean name =resourceBundleLocatorclass =org.springframework.context.support.ReloadableResourceBundleMessageSource>
< property name =basenames>
< list>
<值> / WEB-INF / validationMessages< /值>
< / list>
< / property>
< / bean>



/WEB-INF/validationMessages.properties:

  location.title.notEmpty =我的自定义消息

Form(Class Location)

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

这里出了什么问题?

解决方案

明白了! : - )



我在 dispatcher-servlet.xml 中添加了以下两个bean, / p>

 < bean id =messageSourceclass =org.springframework.context.support.ReloadableResourceBundleMessageSource> 
< property name =basenamevalue =/ WEB-INF / validationMessages/>
< / bean>

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

  NotEmpty.location.title =我的自定义消息

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

一般来说,消息文件的语法应该看起来像


$ b $ pre $ [ConstraintName]。[ClassName]。[FieldName] = [Message]



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

一个弹簧控制器只需添加 @Autowired private MessageSource messageSource; 作为类字段并使用 messageSource.getMessage 方法。因为我只使用了一个使用 messageSource.getMessage(NotEmpty.location.title,null,null)的语言环境


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?

解决方案

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 )

这篇关于在Hibernate + Spring中使用自定义验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆