Spring 3 mvc namespace and i18n
Problem with i18n and Spring 3 mvc namespace
I have not figured out how to get messages resolved when using Spring’s mvc namespace.
For example, a JSP with this line:
<fmt:message key="welcome.title"/>
shows:
???welcome.title???
I have a messages directory under WEB-INF with messages.properties.
Here is the web-servlet.xml (my dispatcher servlet is named web). Any help very much appreciated.
<!-- Scans for @Controllers to deploy as beans -->
<context:component-scan base-package="com.mylittlecompany.web.controllers" />
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="1" />
</bean>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="o开发者_Go百科rg.springframework.web.servlet.i18n.CookieLocaleResolver" />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Only relevant log file entry:
DEBUG Thread-1 org.springframework.web.context.support.XmlWebApplicationContext - Using MessageSource [org.springframework.context.support.ReloadableResourceBundleMessageSource: basenames=[/WEB-INF/messages/messages]]
ResourceBundle is now looking for your files in package "/WEB-INF/messages" that doesn't exist.
Try to put your "messages" directory to WEB-INF/classes and replace messageSource bean with:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="messages/messages" />
<property name="cacheSeconds" value="1" />
</bean>
Put the following code in applicationContext.xml
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basenames">
<beans:list>
<beans:value>messages</beans:value>
</beans:list>
</beans:property>
</beans:bean>
Put your messages.properties into WEB-INF/classes
And in your jsp you can refer messages like
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<input name="reset" type="reset" value='<spring:message code="button.reset"/>' onclick="setLoginFocus()" />
And your messages.properties
button.reset=ResetFields
Actually, the problem is WEB-INF is NOT on classpath, so your messages.properties in not getting picked up.
Put it under WEB-INF/classes or WEB-INF/lib (though they are not good places to put .properties files).
I'd suggest you to put it under :-
src/main/resources/ OR (src/main/resources/META-INF)
- SE
EDIT
<property name="basename" value="messages/messages" />
I recently completed a Spring 3 project with extensive localization, and used the spring:message tag instead of fmt:message. You might try spring:message to see if the behavior changes. It should find your message source and resolve the message key by the current locale.
精彩评论