Max ZooZoo Max ZooZoo - 6 months ago 24
Java Question

Where to put text properties?

I'm trying to do localization for my Java web application and I am getting an error. I am using Maven for dependencies and I am using Google app engine (Jetty web server). The application is using spring framework. Here is what I have so far:

Project structure:

enter image description here

messages_en_US.properties

label.firstname=First Name
label.lastname=Last Name


index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<t:genericpage>
<jsp:attribute name="title">
Welcome
</jsp:attribute>
<jsp:body>
<script type="text/javascript" src="../static/js/index.js"></script>
<div class="flex_container_column">
<spring:message code="label.firstname" />
<br>
<spring:message code="label.lastname" />
<br>
</div>
</jsp:body>
</t:genericpage>


spring-mvc-servlet.xml

<mvc:resources mapping="/static/**" location="/static/" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- Resource bundle message: Start -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>

<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>

<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<!-- Resource bundle message: End -->


Error

WARNING: Nested in javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.jsp.JspTagException: No message found under code 'label.firstname' for locale 'en_US'.:
javax.servlet.jsp.JspTagException: No message found under code 'label.firstname' for locale 'en_US'.
at org.springframework.web.servlet.tags.MessageTag.doEndTag(MessageTag.java:200)
at org.apache.jsp.index_jsp._jspx_meth_spring_005fmessage_005f0(index_jsp.java:110)
at org.apache.jsp.index_jsp.access$0(index_jsp.java:97)
at org.apache.jsp.index_jsp$Helper.invoke1(index_jsp.java:176)
at org.apache.jsp.index_jsp$Helper.invoke(index_jsp.java:205)


pom.xml

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>


I tried few things:


  1. Moved the messages to
    /WEB-INF
    but I got the same error when I changed
    <property name="basename" value="/WEB-INF/messages" />
    . But as per my understanding, if I do so, then I might not be able to access this in the code. That is why I put it into resources initially.

  2. I removed en_US from the default messages property file name, but I got the same error.



Appreciate your help on where and how to setup the project property files + the text.properties.

Answer

I ended up putting the messages.properties in /WebContent/WEB-INF/messages/messages.properties and the reason I was getting the error was because I have 2 contexts defined:

  1. applicationContext.xml
  2. spring-mvc-servlet.xml

I was putting the configuration in spring-mvc-servlet.xml, which was not the right place. Instead, it should have been in applicationContext.xml (the parent context). I solved the issue but then I faced another problem, LocaleChangeInterceptor is not intercepting, so the language can't be changed! However, the problem is resolved.