Click to See Complete Forum and Search --> : [RESOLVED] I18N under Spring framework : can't reach messsages.properties


therollinone
05-08-2008, 01:30 PM
Hi there, I'm trying to add Internationalization feature to my website but it seems I just can't reach the "messages.properties" file. All I get in my jsp page is ???heading??? or such. I'll try to give you an overview of my website structure and files see if you can find what I'm doing wrong ...

Btw, Im using Apache Tomcat 5.5 (if it has any impact, I doubt so ...) and I'm implementing the site using Spring model.

Website structure : (found in the webapps folder of Tomcat)

<PROJECT>
....index.jsp
....<WEB-INF>
........project-servlet.xml
........web.xml
........<CLASSES>
............messages.properties
............<BUS>
................project java classes (business logic)
............<WEB>
................project java classes (web logic)
........<JSP>
................hello.jsp (file from wich I can't access messages.properties)
........<LIB>
................all ext. libraries (.jar)

Files content : (only parts I think have an impact, hopefully I didn't forget any :confused: )

hello.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head><title><fmt:message key="title"/></title></head>
<body>
<h1><fmt:message key="heading"/></h1>
... ... ...
</body>
</html>

project-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

... ... ...

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

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>

messages.properties
title=SpringApp
heading=Hello :: SpringApp


That's about it, hope I didn't forget any files content, anyway if i did just tell me and I'll post them.

Hope you guys can find what's wrong or what I'm missing cause I've been trying to get that right for 3 days now. Moving files around, modyfing code here and there but nothing gave me any satisfying results ... :(

Thanks a lot

chazzy
05-08-2008, 04:25 PM
what does your web.xml look like? anything out of the ordinary?

therollinone
05-08-2008, 06:18 PM
what does your web.xml look like? anything out of the ordinary?

Nothing exceptionnal, here it is :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>

<web-app>

<servlet>
<servlet-name>project</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>project</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


Hope this helps

chazzy
05-08-2008, 06:48 PM
Ah right. Where are you telling spring to read bean defs from project-servlet.xml ?

therollinone
05-08-2008, 07:11 PM
I'm not doing it it seems ...

Before posting in the forum I've read a lot of stuff about how to add the "bundle" to a page but everything I've tried didn't work so I concluded it was something else. Guess I was wrong ...

I've seen that I could add it directly in each jsp page like :

<f:loadBundle basename="messages" var="bundle" />
... or ...
<fmt:setBundle basename="project.props"/>
...or ...
<fmt:bundle basename="messages">

But, everything I've tried didn't work out. Could you give me a short example of the line(s) of code to add.

Also, with this approach I would have to add the code in each and every page, is there a way to set a bundle for all pages or multiple bundles to different subsets of pages ?

Thanks a lot

chazzy
05-08-2008, 07:22 PM
and what happens if you just put this on your JSP page?


<fmt:bundle basename="messages">
Title: <fmt:message key="title" />
</fmt:bundle>


In JSP, if you're using the format namespace you need to wrap the values.

therollinone
05-09-2008, 12:56 PM
Hmmm, didn't work. I've tried the following :

Wrapping a single fmt call :

<fmt:bundle basename="messages">
<h1><fmt:message key="heading"/></h1>
</fmt:bundle>

Wrapping the whole body :

<html>
<head><title><fmt:message key="title"/></title></head>
<fmt:bundle basename="messages">
<body>
<h1><fmt:message key="heading"/></h1>
</body>
</fmt:bundle>
</html>

Wrapping the whole html tags :

<fmt:bundle basename="messages">
<html>
<head><title><fmt:message key="title"/></title></head>
<body>
<h1><fmt:message key="heading"/></h1>
</body>
</html>
</fmt:bundle>


And I was wondering, isn't that code from project-servlet.xml suppose to actually do the job ? If not, does it have any purpose ...

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


Anyway, thanks a lot for your help so far, I keep reading/searching for a solution :)

chazzy
05-09-2008, 02:37 PM
In the current setup, spring's not doing anything. but that's fine, let's leave it for now.

I do want to check that you have everything setup right. One thing I do see missing is your taglib declarations. It's not enough to just prefix a tag w/ fmt, you need to also define it. You might want to start with this article:

http://www.javaworld.com/javaworld/jw-02-2003/jw-0228-jstl.html?page=1

It's older, but so is the fmt taglib.

therollinone
05-10-2008, 01:54 PM
It seems like I've cut too many lines when I posted my code (trying to make it shorter). In my hello.jsp I have this code on the first line :
<%@ include file="/WEB-INF/jsp/include.jsp" %>
include.jsp
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
This include seems to work. I can do a <c:_out value="JSTL is working!" /> and it works fine. I've also tried to use fmt:messages instead of message and I get an error stating that there is no such tag in the TLD so I guess the TLD is loaded correctly.

I've read about the JSLT installation and it should be done correctly. I already had both .jar (jstl.jar / standard.jar) in \webapps\project\WEB-INF\lib and as I said earlier the calls to the core library works (c:_out) so I don't know why calls to fmt:message don't.

So far, from what I can see, the only thing that tells where or what to look for when it comes to the messages.properties file is in the project-servlet.xml and all I can see is the code below found in web.xml that refers to the servlet file. Is that all what is required to access it?
<servlet>
<servlet-name>project</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

Hope this will help ...

P.S. : I've used c:_out but I know there isn't any "_", had to do it or I was getting an annoying :o