-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
James Cook opened SPR-7392 and commented
I was having a great deal of trouble getting spring:message tag to recognize any keys from my resource bundles. When I changed my resource bundle from ReloadableResourceBundleMessageSource to ResourceBundleMessageSource, everything suddenly worked fine.
Obligatory code:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
{code|messages.properties}
site.title=My Title
```html
<%@ page import="org.springframework.web.servlet.support.RequestContext" %>
<%@ page import="org.springframework.web.servlet.support.RequestContextUtils" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<fmt:setBundle basename="WEB-INF/classes/messages"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Simple jsp page</title></head>
<body>
<p>Message from context: <c:out value="${message}" /></p>
<p>Locale: <%= RequestContextUtils.getLocale(request) %></p>
<p>Going Direct: <%= RequestContextUtils.getWebApplicationContext(request).
getMessage("site.title", null, RequestContextUtils.getLocale(request)) %></p>
<p>JSTL Format Tag: <fmt:message key="site.title" /></p>
<p>Spring Message Tag: <spring:message code="site.title" /></p>
</body>
</html>
Message from context: Hello World!
Locale: en_US
Going Direct: site.title
JSTL Format Tag: ???site.title???
Spring Message Tag: site.title
You can see that the spring:message tag and going after the message source directly results in no message being resolved. Same goes for the JSTL format tag. Now if I only change the message source to ResourceBundleMessageSource, spring:message works as I would of originally expected:
Message from context: Hello World!
Locale: en_US
Going Direct: My Title
JSTL Format Tag: ???site.title???
Spring Message Tag: My Title
Also, I just wanted to confirm that the definition of the messageSource bean requires that it have an id = "messageSource"? When I remove the id (which I tend to do for beans I won't be looking up), no messages are resolved.
Affects: 3.0.3
Reference URL: http://forum.springsource.org/showpost.php?p=310752&postcount=7
Issue Links:
- ResourceBundleThemeSource could expose the property fallbackToSystemLocale of ResourceBundleMessageSource [SPR-13209] #17801 ResourceBundleThemeSource could expose the property fallbackToSystemLocale of ResourceBundleMessageSource
Referenced from: commits abf341c, c81543e
3 votes, 4 watchers