【问题标题】:Spring 3 mvc namespace and i18nSpring 3 mvc 命名空间和 i18n
【发布时间】:2011-03-05 11:40:43
【问题描述】:

i18n 和 Spring 3 mvc 命名空间的问题

我还没有弄清楚如何在使用 Spring 的 mvc 命名空间时解析消息。

例如,有这行的 JSP:

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

显示:

???welcome.title???

我在 WEB-INF 下有一个带有 messages.properties 的消息目录。

这是 web-servlet.xml(我的调度程序 servlet 名为 web)。非常感谢任何帮助。

<!-- Scans for @Controllers to deploy as beans -->
<context:component-scan base-package="com.mylittlecompany.web.controllers" />

<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />

<!-- Configures Handler Interceptors -->
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>

<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages/messages" />
    <property name="cacheSeconds" value="1" />
</bean>

<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

仅相关的日志文件条目:

DEBUG Thread-1 org.springframework.web.context.support.XmlWebApplicationContext - Using MessageSource [org.springframework.context.support.ReloadableResourceBundleMessageSource: basenames=[/WEB-INF/messages/messages]]

【问题讨论】:

    标签: spring spring-mvc internationalization


    【解决方案1】:

    ResourceBundle 现在正在“/WEB-INF/messages”包中查找不存在的文件。

    尝试将您的“消息”目录放入 WEB-INF/classes 并将 messageSource bean 替换为:

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

    【讨论】:

      【解决方案2】:

      将以下代码放入applicationContext.xml中

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

      把你的messages.properties放到WEB-INF/classes中

      在你的 jsp 中你可以参考类似的消息

      <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
      <input name="reset" type="reset" value='<spring:message code="button.reset"/>' onclick="setLoginFocus()" />
      

      还有你的messages.properties

      button.reset=ResetFields
      

      【讨论】:

        【解决方案3】:

        实际上,问题是 WEB-INF 不在类路径中,因此您的 messages.properties 没有被拾取。

        将它放在 WEB-INF/classes 或 WEB-INF/lib 下(尽管它们不是放置 .properties 文件的好地方)。

        我建议你把它放在:-

        src/main/resources/ OR (src/main/resources/META-INF)

        • SE

        编辑

        <property name="basename" value="messages/messages" />
        

        【讨论】:

          【解决方案4】:

          我最近完成了一个包含大量本地化的 Spring 3 项目,并使用了 spring:message 标签而不是 fmt:message。您可以尝试 spring:message 以查看行为是否发生变化。它应该找到您的消息源并通过当前语言环境解析消息键。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-04-22
            • 1970-01-01
            • 1970-01-01
            • 2011-11-19
            • 2021-10-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多