Spring boot 2.0.1中的NoSuchMessageException

时间:2018-05-06 19:54:23

标签: java spring

我正在尝试在我的网站上实现国际化,但仍然会遇到此异常:

org.springframework.context.NoSuchMessageException: No message found under code 'nav_home' for locale 'en'

我的资源包位于:

src/main/resources/translations

Project structure [imgur]

这是我的配置文件:

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访问任何消息时,我仍然遇到同样的错误。

请帮助我stackoverflow,你是我唯一的希望;)

1 个答案:

答案 0 :(得分:0)

没关系,我发现了我的错误。

在我的配置文件中,我的ResourceBundleMessageSource bean没有命名为messageSource()

那是 - '