[英]NoSuchMessageException in Spring boot 2.0.1
I am trying to implement internationalization in my website but keep getting this exception : 我正在尝试在我的网站中实现国际化,但始终收到此异常:
org.springframework.context.NoSuchMessageException: No message found under code 'nav_home' for locale 'en'
My resource bundle is located here : 我的资源包位于这里:
src/main/resources/translations
Project structure [imgur] 项目结构[imgur]
Here is my configuration file : 这是我的配置文件:
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);
}
}
My controller : 我的控制器:
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";
}
}
And my general_en.properties file : 还有我的general_en.properties文件:
nav_home=Home
nav_books=All books
nav_about=About us
nav_account=Account
nav_cart=Cart
I've been searching for hours but cannot find anything that helps me I've modifying my basenames in my configuration class by adding 我一直在搜索几个小时,但是找不到任何可以帮助我的事情,通过在配置类中添加
-> classpath:
-> classpath*:
-> / before translations, with and without classpath(*)
I get the same error when using the fr locale. 使用fr区域设置时出现相同的错误。
I still get the same error when I try to access any message from template.jsp instead of HomeController. 当我尝试从template.jsp而不是HomeController访问任何消息时,仍然出现相同的错误。
Please help me stackoverflow, you're my only hope ;) 请帮助我stackoverflow,您是我唯一的希望;)
Nevermind, I found my error. 没关系,我发现了我的错误。
In my configuration file, my ResourceBundleMessageSource bean wasn't named messageSource() 在我的配置文件中,我的ResourceBundleMessageSource bean没有命名为messageSource()
That's it --' 而已 - '
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.