What is wrong with my Spring MessageSource setup?
I'm attempting to set up a ReloadableResourceBundleMessageSource to use in my JSPs and Controller. Here's the pieces I have. The message key is printing to the screen in my jsp. What am I missing?
spring-servlet.xml
<bean id="messages" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<value>/WEB-INF/messages/login</value>
</property>
</bean>
login_en.properties
login.title=Welcome to the login page.
home.jsp
<body>
<spring:message code="login.title" text="login.title"></spring:message>
</bod开发者_StackOverflow社区y>
Yeah, this tripped me up for a long time too. The bean id has to be called messageSource.
I am posting my answer for java-based configuration because I ran into the same problem when I switched from xml to a java-based config application and it was a total pain in the a** to figure out why it wasn't working. (Turns out for me, the was the same issue @jiggy pointed out... wouldn't you know...)
@Bean(name = "validator")
public LocalValidatorFactoryBean getLocalValidatorFactoryBean() {
final LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(this.getMessageSource());
validator.afterPropertiesSet();
return validator;
}
@Bean(name = "messageSource") // --> !!! This is what is so important!!!
public MessageSource getMessageSource() {
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); // easily swapped out with "ReloadableResourceBundleMessageSource", my app just doesn't have any necessary reloadable message requirements.
messageSource.setBasenames(ERROR_MESSAGE_DIRECTORY); // static String for a direct path to your "ValidationMessages.properties" file or whatever name you've given it.
return messageSource;
}
And also don't forget to declare the messageSource application wide and not in servlet spring beans.
精彩评论