Java Spring boot 2.0.1中的NoSuchMessageException
我正在尝试在我的网站中实现国际化,但不断遇到以下例外情况:Java Spring boot 2.0.1中的NoSuchMessageException,java,spring,Java,Spring,我正在尝试在我的网站中实现国际化,但不断遇到以下例外情况: org.springframework.context.NoSuchMessageException: No message found under code 'nav_home' for locale 'en' 我的资源包位于此处: src/main/resources/translations 这是我的配置文件: package com.spring.henallux.FlourishBlotts.configuration;
org.springframework.context.NoSuchMessageException: No message found under code 'nav_home' for locale 'en'
我的资源包位于此处:
src/main/resources/translations
这是我的配置文件:
package com.spring.henallux.FlourishBlotts.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import java.util.Locale;
@Configuration
public class MainConfiguration implements WebMvcConfigurer {
@Bean
public DefaultMessageCodesResolver defaultMessageCodesResolver() {
DefaultMessageCodesResolver defaultMessageCodesResolver = new DefaultMessageCodesResolver();
return defaultMessageCodesResolver;
}
@Bean
public ResourceBundleMessageSource resourceBundleMessageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setDefaultEncoding("UTF-8");
messageSource.setBasenames("translations/general", "translations/errors");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(new Locale("en"));
resolver.setCookieName("flourish-blotts_localeCookie");
resolver.setCookieMaxAge(-1);
return resolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("locale");
registry.addInterceptor(interceptor);
}
}
我的控制器:
package com.spring.henallux.FlourishBlotts.controller;
import com.spring.henallux.FlourishBlotts.model.LoginForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Locale;
@Controller
@RequestMapping(value="/home")
public class HomeController {
private final MessageSource messageSource;
@Autowired
public HomeController(MessageSource messageSource) {
this.messageSource = messageSource;
}
@RequestMapping(method = RequestMethod.GET)
public String home (Model model, Locale locale){
model.addAttribute("title", messageSource.getMessage("nav_home", null, locale));
model.addAttribute("path", "home");
model.addAttribute("loginForm", new LoginForm());
return "integrated:home";
}
@RequestMapping(value="/login", method = RequestMethod.POST)
public String login (@ModelAttribute(value="loginForm") LoginForm form){
//TODO log user here
return "redirect:/home";
}
}
和我的general_en.properties文件:
nav_home=Home
nav_books=All books
nav_about=About us
nav_account=Account
nav_cart=Cart
我已经找了几个小时了,但找不到任何对我有帮助的东西
我已经在配置类中通过添加
-> classpath:
-> classpath*:
-> / before translations, with and without classpath(*)
我在使用fr语言环境时也会遇到同样的错误
当我尝试从template.jsp而不是HomeController访问任何消息时,仍然会遇到相同的错误
请帮帮我,你是我唯一的希望;) 不管怎样,我发现了我的错误 在我的配置文件中,我的ResourceBundleMessageSourcebean没有命名为messageSource()
就这样——”你把事情弄得太复杂了。删除
MessageSource
和DefaultMessageCodesResolver
的bean,只需将spring.messages.basename=translations/general,translations/errors
放入应用程序.properties
。Spring Boot已经为您提供了一个MessageSource
,只需适当地配置它(使用框架,而不是围绕框架)。