gpt4 book ai didi

java - Java 配置中的 Spring Mvc 消息包

转载 作者:行者123 更新时间:2023-12-02 02:19:05 26 4
gpt4 key购买 nike

我使用 spring 5.1.5。我使用 Java 配置,但消息束不起作用。

  1. 当我用 xml 编写它时,只要将配置传输到 Java,一切都会正常工作,出现消息 No message found under code 'jsp.header.entry' for locale 'ru'
  2. 在 xml 配置中我可以设置 fileEncodings("UTF-8")但在 Java 配置中我不知道如何做到这一点

强烈请求帮助。谢谢。

原来是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/locales/message"/>
<property name="useCodeAsDefaultMessage" value="false"/>
<property name="fileEncodings" value="UTF-8"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="-1"/>
</bean>

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

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

变成了

@Configuration
@EnableWebMvc
@ComponentScan({"com.seeds.controllers", "com.seeds.validators"})
public class WebConfig implements WebMvcConfigurer {

//Message Source config
@Bean
public ReloadableResourceBundleMessageSource getMessageSource(){
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

messageSource.setBasename("/WEB-INF/locales/message");
messageSource.setUseCodeAsDefaultMessage(false);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(-1);
return messageSource;
}

@Bean
public LocaleChangeInterceptor getLocaleChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();

localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}

@Bean
public SessionLocaleResolver getLocaleResolver(){
SessionLocaleResolver localeResolver = new SessionLocaleResolver();

localeResolver.setDefaultLocale(new Locale("ru"));
return localeResolver;
}

//others

最佳答案

MessageSourceLocaleResolver 均按名称查找,分别为 messageSourcelocaleResolver

使用基于 Java 的配置时 the name of the method is used as the name of the bean 。由于您已将它们命名为 getMessageSourcegetLocaleResolver,因此找不到所需的 bean,因此被忽略。

要修复此问题,请将名称分别更改为 messageSourcelocaleResolver

@Configuration
@EnableWebMvc
@ComponentScan({"com.seeds.controllers", "com.seeds.validators"})
public class WebConfig implements WebMvcConfigurer {

//Message Source config
@Bean
public ReloadableResourceBundleMessageSource messageSource(){

ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/locales/message");
messageSource.setUseCodeAsDefaultMessage(false);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(-1);
return messageSource;
}

@Bean
public SessionLocaleResolver localeResolver(){

SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("ru"));
return localeResolver;
}

关于java - Java 配置中的 Spring Mvc 消息包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57286150/

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