java - Spring 不使用默认语言环境中的源

标签 java spring spring-mvc

我已经在 spring 配置中更改了默认语言环境,但是 spring 总是使用 messages_en.properties 而不是 messages.properties。看来 Spring 忽略了我对语言环境的选择。

定义的位置:

messages.properties
messages_en.properties

Spring 配置: application-context.xml

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
  <property name="defaultLocale" value="cs"/>
</bean>

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

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
    <list>
      <value>messages</value>
    </list>
  </property>
  <property name="defaultEncoding" value="UTF-8" /> 
</bean>

servlet-context.xml

<mvc:interceptors>
 <mvc:interceptor>
  <mvc:mapping path="/**" />
  <exclude-mapping path="/admin/**"/>
  <exclude-mapping path="/image/**"/>
  <exclude-mapping path="/ajax/**"/>
  <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>

在JSP页面中

<spring:message code="csn.terminology.csnId" />
<p>Current Locale : ${pageContext.response.locale}</p> 
<!-- output is 'cs', but messages are from messages_en.properties file --> 

项目中使用了Spring Framework 3.2.4 预先感谢您的帮助。

最佳答案

如果它是完整的配置那么你忘记添加语言环境解析器 您可以像这样添加一个 SessionLocaleResolver 并设置默认的语言环境属性

<bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en"></property>
    </bean>

关于java - Spring 不使用默认语言环境中的源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18945978/

相关文章:

java - 使用 Spring MVC 创建 Web 应用程序

java - 错误处理的 Spring MVC Rest 服务 Controller 正确吗?

spring-mvc - Spring不注入(inject)ServletContext

java - 用 Java 对字符串 URL 进行编码

java - 无法将信息从hibernate推送到mysql数据库

java - 为什么要将上下文传递给 Intent 的构造函数?

java - 无法解析类型 org.springframework.beans.BeansException。它是从所需的 .class 文件中间接引用的

java - 传递对 Activity 的引用

spring - 关于 Spring quartz 的几个问题

java - 一对多的连接列为 null hibernate