Use custom validation messages in Hibernate + Spring

前端 未结 1 1851
不思量自难忘°
不思量自难忘° 2020-12-09 22:04

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

But still just getting {location.title.notEmpty} as

相关标签:
1条回答
  • 2020-12-09 22:15

    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 )

    0 讨论(0)
提交回复
热议问题