Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="org.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]]
like image 211
Francois Avatar asked Oct 15 '22 04:10

Francois


2 Answers

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>
like image 195
Aleksey Otrubennikov Avatar answered Nov 03 '22 22:11

Aleksey Otrubennikov


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
like image 41
Satish Avatar answered Nov 03 '22 22:11

Satish