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: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.getMessagemethods. Because I’m just using one locale I usedmessageSource.getMessage( "NotEmpty.location.title", null, null )