更改Hibernate验证器的属性文件名



我不想使用ValidationMessages。属性来保存我的应用程序的消息。我想使用其他所有东西使用:LabelNames.properties.

有什么办法吗?

这是可能的。您可以使用ResourceBundleMessageInterpolator及其bundle定位器API。自定义包名可以像这样指定:

Validator validator = 
   Validation.byProvider(HibernateValidator.class)
      .configure()
      .messageInterpolator(
         new ResourceBundleMessageInterpolator(
            new PlatformResourceBundleLocator( "LabelNames" )))
      .buildValidatorFactory()
      .getValidator();

使用Spring时,请参考本答案中描述的从LocalValidatorFactoryBeanMessageSource的所需包。

相关内容

最新更新