8

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?

2
  • I do not know if this could cause that problem, but make sure that the message properties file has an empty line as last line. Commented Feb 10, 2012 at 22:22
  • nope that wasn't the problem, last empty line isn't needed Commented Feb 10, 2012 at 22:33

1 Answer 1

14

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 )

Sign up to request clarification or add additional context in comments.

4 Comments

Nice, but it works also for Hibernate validation annotations :)
Yes, my first thought was it works only for hibernate validation annotations but it works for both - hibernate and javax.
It's not necessary to adjust the syntax when specifying the validator in <mvc:annotation-driven validator="validator" />, see stackoverflow.com/a/21508937/1672678 and stackoverflow.com/a/22655303/1672678.
If you are using Spring Boot, you can take advantage of autoconfiguration by adding the following key to application.properties: spring.messages.basename=your/basename/messages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.