2

我正在使用弹簧 3.1.2 MVC。我正在尝试通过注释配置 Web 应用程序

在这里,我想实现国际化,因为我正在使用LocaleChangeInterceptorReloadableResourceBundleMessageSourceSessionLocaleResolver类。这是我的配置类:

@Configuration
@EnableWebMvc 
public class MyAppConfig extends WebMvcConfigurerAdapter{

@Bean
public ReloadableResourceBundleMessageSource resourceBundleMessageSource(){
        ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
        String[] resources= {"classpath:labels","classpath:message"};
        messageSource.setBasenames(resources);
        return messageSource;
    }

@Bean 
public LocaleChangeInterceptor localeChangeInterceptor(){
    LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("locale");
    return localeChangeInterceptor;
}

@Bean
public SessionLocaleResolver sessionLocaleResolver(){
    SessionLocaleResolver localeResolver=new SessionLocaleResolver();
    localeResolver.setDefaultLocale(new Locale("da","DK"));
    return localeResolver;
}   

public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

}

但这不起作用它显示默认文本

<spring:message code="login.user.password" text="Password" /> 

作为“密码”。

请帮我。当我在其中进行以下配置时,applicationContext.xml它工作正常。

 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">


 <property name="basenames">
    <list>
        <value>classpath:message</value>
        <value>classpath:labels</value>            
    </list>
</property>     
<property name="defaultEncoding" value="ISO-8859-1" />
</bean>

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


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

请帮我解决这个问题。

4

2 回答 2

3

messageSource 的 bean 名称必须是“messageSource”,使用 @Configuration 您将其命名为resourceBundleMessageSource. 将其更改为:

    @Bean
    public ReloadableResourceBundleMessageSource messageSource(){
        ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
        String[] resources= {"classpath:labels","classpath:message"};
        messageSource.setBasenames(resources);
        return messageSource;
    }
于 2012-07-24T09:34:57.003 回答
2

我解决了我的问题,我的 web.xml 中缺少初始化参数条目

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.config.AppConfig
        </param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

添加此条目后,我的带有注释的配置工作正常。:)

于 2012-08-01T12:23:08.757 回答