Spring MVC 3 I18N / MessageSource无法正常工作

时间:2013-01-03 18:57:01

标签: java spring-mvc internationalization spring-3

我正在尝试在Spring MVC中配置internazionalization(使用通过链接更改locales),但是,它似乎根本不起作用:默认语言环境由于某种原因总是ru,尽管默认设置为en,它们不会使用链接进行更改,无论选择的语言环境如何,spring消息都显示为空(messages_de,messages_en和messages_ru.properties都存在于类路径(src / main / resources)中)。它们包含例如。

label.test=Russian 

我将它们称为

<spring:message code="label.test" /> 

在我的JSP中。它们没有像那样显示。 我认为即使没有错误或警告,也找不到messageSource。我真的很感激任何帮助,因为我试图弄清楚很长一段时间。显然,我错过了一些细节,但我肯定无法解决问题。这是我的配置文件(或最相关的部分)。

根的context.xml         

<context:component-scan base-package="... .dao" />
<context:component-scan base-package="... .service" />

<import resource="data.xml" />
<import resource="security.xml" />

MVC-调度-servlet.xml中

<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

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

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="sitelocale" />
    </bean>
</mvc:interceptors>

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

data.xml中     

<!-- Transaction Manager -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<!-- ////////////////////////////////////////////////////////////////////////// 
    " -->

<bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource"  
    destroy-method="close">
     <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.databaseurl}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />   
</bean>

<!-- ////////////////////////////////////////////////////////////////////////// -->

<!-- Hibernate SessionFactory configuration -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.tsystems.javaschool.kts.domain" />
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>     
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
        </props>
    </property>
</bean>

的web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring MVC -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>






<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

3 个答案:

答案 0 :(得分:0)

您不需要将basename值加上“classpath:”前缀,请尝试按以下方式更改它:

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

答案 1 :(得分:0)

添加以下内容应该允许您使用名为“lang”的URL参数(即lang = en)更改lang。这将允许您覆盖浏览器默认设置并显式声明正在使用的语言。

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

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>

另外,如果要使用classpath:messages作为基本名称,请在已部署的webapp中确保消息文件位于WEB-INF / classes /下。

答案 2 :(得分:0)

同时检查是否在顶部添加了

<%@ page contentType="text/html;charset=UTF-8" %>