【问题标题】:Internationalization in Spring MVCSpring MVC 中的国际化
【发布时间】:2014-03-01 12:09:50
【问题描述】:

我正在 Spring MVC 中实现内部化。下面是我的 applicationcontext xml 文件

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

我已将 ApplicationResources_en.properties 文件保存在 /Webcontent/WEB-INF/ 目录下。

在 JSP 中,我使用 &lt;fmt:message&gt; 标签加载消息。但我收到如下消息。
???addEmployee.UserName???

有人可以帮帮我吗?

【问题讨论】:

    标签: spring-mvc internationalization


    【解决方案1】:

    确保你注册了一个LocaleChangeInterceptor bean 来拦截用户的Locale:

    <mvc:interceptors>    
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
          p:paramName="locale" />
    </mvc:interceptors>
    

    然后尝试使用&lt;spring:message code="addEmployee.UserName" text="username" /&gt; 代替标签。

    否则,&lt;spring:message&gt;&lt;fmt:message&gt; 非常相似,只是后者不使用 Spring 语言环境解析器,您必须将解析器更改为

    `<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>` 
    

    这将为 JSTL 标记设置 LocalizationContext。

    BR。

    【讨论】:

    • 我不能用 标签代替 吗?使用 标签有什么具体原因吗?
    • 实际上是的,您可以肯定地使用&lt;fmt:message&gt; 标签,但我已经有一段时间没有碰它了,我记得除非您为您的 jsp 文件指定视图解析器,否则它不会起作用&lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt; 实际上会设置 LocalizationContext 并为你做所有的事情。
    • 我已将视图配置为“InternalResourceView”。现在我改为“JstlView”。它的工作!非常感谢:)
    • 不客气,我将编辑我的答案,以便为遇到相同问题的开发人员提供它(请随意投票,使其成为值得信赖的答案:) )。
    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多