finn0013
finn0013

Reputation: 195

Spring MVC localization - properties file not being found

I'm trying to get basic localization working in a new Spring MVC app. I've tried everything I can think of but always end up with the following exception, regardless of what I do. Any help would be greatly appreciated...

I've tried adding the properties file to every single directory but it still gives me an error. Once I get it working I'll systematically remove extras.

The exception:

07/13/2012 21:06:00.178 [DEBUG] [org.springframework.context.support.ReloadableResourceBundleMessageSource] No properties file found for [messages] - neither plain properties nor XML
07/13/2012 21:06:00.178 [DEBUG] [org.springframework.context.support.ReloadableResourceBundleMessageSource] No properties file found for [messages_en] - neither plain properties nor XML
07/13/2012 21:06:00.179 [DEBUG] [org.springframework.context.support.ReloadableResourceBundleMessageSource] No properties file found for [messages_en_US] - neither plain properties nor XML
07/13/2012 21:06:00.182 [ERROR] [org.springframework.web.servlet.tags.MessageTag] No message found under code 'test.testMessage' for locale 'en_US'.
javax.servlet.jsp.JspTagException: No message found under code 'test.testMessage' for locale 'en_US'.

Here's the JSP entry (the fmt:message just shows ???test.testMessage??? while the spring:message blows up):

<h2><fmt:message key="test.testMessage" />!</h2>
<h2><spring:message code="test.testMessage" />!</h2>

Here's the configuration in my comparison-servlet.xml file:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="messages" />
</bean>

Here's the war structure (note that I added the messages.properties at pretty much every level):

messages.properties
src
    messages.properties
    main
        messages.properties
        java
        ...
        resources
            messages.properties
        webapp
            index.jsp
            messages.properties
            WEB-INF
                comparison-servlet.xml
                web.xml
                messages.properties
                jsp
                messages.properties
                    compare.jsp
                    globalIncludes.jsp
            classes
                messages.properties
            resources
                messages.properties
    test
        ...

Any idea why it can't find a file that is definitely there? Do I need to explicitly set something regarding the classpath?

Upvotes: 0

Views: 8573

Answers (2)

finn0013
finn0013

Reputation: 195

The problem turned out to be with the war file itself. While the properties files were being copied to the target directory and showed up as if they were included in the build, the maven pom file was set to explicitly exclude properties files from the built artifact.

Once I removed this from my pom file, everything started working fine:

<configuration>
    <packagingExcludes>**/*.properties</packagingExcludes>
</configuration>

Upvotes: 0

jelies
jelies

Reputation: 9290

Try to put messages.properties under WEB-INF directory inside any folder and set the path with basename property.

For example:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/i18n/messages" />
</bean>

See Javadoc here.

Upvotes: 4

Related Questions