gpt4 book ai didi

java - Spring MVC 3 I18N/MessageSource 不工作

转载 作者:行者123 更新时间:2023-12-01 04:55:56 26 4
gpt4 key购买 nike

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

label.test=Russian 

我将它们称为

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

在我的 JSP 中。它们并没有这样显示。我认为即使没有错误或警告,也没有找到 messageSource。我真的很感激任何帮助,因为我很长时间都在努力解决这个问题。显然,我错过了一些细节,但我绝对无法捕获问题所在。这是我的配置文件(或最相关的部分)。

根上下文.xml

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

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

mvc-dispatcher-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>

数据.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>

最佳答案

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

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

关于java - Spring MVC 3 I18N/MessageSource 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14145335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com