【问题标题】:Internationalization using Spring ResourceBundleMessageSource and FMT使用 Spring ResourceBundleMessageSource 和 FMT 进行国际化
【发布时间】:2013-10-16 13:15:21
【问题描述】:

我们正在尝试使用 Spring ResourceBundleMessageSource 和 FMT 来实现国际化。但是当我们在 JSP 中使用它时,页面将值显示为 ???message.key???。你能帮我们解决这个问题吗?非常感谢您的回答。

下面是配置:

spring-servlet.xml 条目

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

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

 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>


<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="interceptors">
        <list>             
            <ref bean="localeChangeInterceptor" />                          
        </list>
    </property> 
</bean>

在 JSP 中我们添加了导入的

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:message key="message.key"/>

同样在创建的战争中,我们在 WEB-INF/messages 文件夹下有 msgs.properties 文件。

非常感谢帮助我们识别我们所犯的错误。谢谢你。

【问题讨论】:

    标签: spring spring-mvc internationalization fmtonly


    【解决方案1】:

    我使用的和你一样,我可以使用 fmt:message 从属性文件中检索消息。您可以尝试为此更改资源包吗:

    <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basenames">
                <list>
                    <value>classpath:messages</value>
                </list>
            </property>
            <property name="defaultEncoding" value="UTF-8" />
            <property name="fallbackToSystemLocale" value="false"></property>
        </bean>
    

    属性存储在 src/main/resources 中。

    希望对你有帮助。

    【讨论】:

    • 感谢您的回复。但仍然没有解决问题。我们有多个 spring 上下文和一个 web 应用程序上下文。我们将此 messageSource 定义为 Web 应用程序上下文的一部分。这样对吗?我们是否依赖于 JSP 中的页面编码类型?我们使用的是 3.1.2 发布版的 spring。让我知道你的想法。
    • 会不会对JSTL的版本有依赖?我们使用的是 JBoss 7.1.1 提供的那个。
    【解决方案2】:

    使用 FMT 标签库我们无法解决问题。我相信这与我们在 JBoss 7.1.1 服务器上使用的 jstl jar 和 taglibs 有一些关系。

    我们开始使用 spring tlds 来显示消息。现在一切正常。谢谢您的帮助。

    【讨论】:

      【解决方案3】:

      使用 /WEB-INF/messages/msgs 代替 WEB-INF/messages/msgs

      只需在路径的开头添加“/”即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        • 2019-03-02
        • 1970-01-01
        相关资源
        最近更新 更多