Java Spring boot国际化?lang=en无效

Java Spring boot国际化?lang=en无效,java,spring,spring-mvc,spring-boot,internationalization,Java,Spring,Spring Mvc,Spring Boot,Internationalization,我目前正在使用SpringBoot(包括SpringSecurity)和thymeleaf开发一个Web应用程序。目前,我正试图从整合英语和德语的国际化支持开始 对于基础知识,我遵循了本教程,并尝试让他们的示例发挥作用。 现在,如果我转到Localhost:8443/international并选择URL正确构建到的语言之一…/international?lang=en。Thymeleaf甚至读取.properties文件中标记为默认值的字段。但无论我做什么,我都无法让它真正转换语言 代码: 像

我目前正在使用SpringBoot(包括SpringSecurity)和thymeleaf开发一个Web应用程序。目前,我正试图从整合英语和德语的国际化支持开始

对于基础知识,我遵循了本教程,并尝试让他们的示例发挥作用。 现在,如果我转到Localhost:8443/international并选择URL正确构建到的语言之一…/international?lang=en。Thymeleaf甚至读取.properties文件中标记为默认值的字段。但无论我做什么,我都无法让它真正转换语言

代码:

像这样,我假设它采用默认的messages.properties。但是,如果我将LocaleResolver Bean放入

public class Application extends SpringBootServletInitializer 
类中的主方法所在的位置,它接受在那里设置为默认语言环境的任何语言

从我现在的情况来看,我得出结论:我的.properties文件很好,可以读取,但LocaleChangeInterceptor的某些功能无法正常工作。我进入了调试模式,但是WebConfig类中的任何断点都没有触发

我的一个假设是Spring安全性把事情搞砸了,以至于?lang请求无法解决。(已尝试登录和注销)

如果有人对如何解决这个问题有一些想法,我会非常高兴,谢谢大家提前回复

我的申请类别:

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = UserRepository.class)
@ComponentScan(basePackages = { "my.company.controller", "my.company.Services", "java.lang.String","my.company.Services.Security" })
@EnableConfigurationProperties(my.company.Services.Storage.StorageProperties.class)
public class Application extends SpringBootServletInitializer {

@Autowired
private UserRepository repository;

@Autowired
private SecUserDetailsService userDetailService;


public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}

@Bean
CommandLineRunner init(StorageService storageService) {
    return (args) -> {

        repository.deleteAll();

        userDetailService.addUser("bob", "ross", "admin");
        userDetailService.addUser("1", "1", "superuser");
        userDetailService.addUser("2", "2", "admin");

        System.out.println("All users currently in DB:");
        System.out.println("-------------------------------");
        for (User user1 : repository.findAll()) {
            System.out.println(user1);
        }
        System.out.println();

        // storageService.deleteAll();

        try {
            storageService.init();
        } catch (StorageException e) {
            System.out.println("Ordner schon vorhanden");
        }
    };

//If i add this here french gets picked as default language, changing does still not work
@Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.FRENCH);
        return slr;
    }

}

尝试删除
@ComponentScan
注释。
@springboot应用程序
自动进行组件扫描。我猜您的WebConfig类没有加载。

尝试删除
@ComponentScan
注释。
@springboot应用程序
自动进行组件扫描。我猜您的WebConfig类没有加载。

您真的需要使用
@EnableWebMvc
@EnableAutoConfiguration
?如果我没有弄错的话,它们已经包含在
@springbootcapplication
中,应该位于
应用程序
类中。这的确是事实,感谢您指出!然而,移除它并不能解决我的问题。你知道怎么做吗?你能展示一下你的整个
应用程序吗?另外,您的应用程序的包结构是什么(您将
应用程序放在哪个包中以及您将
网络配置放在哪里?
WebConfig
应用程序
都在同一个包中
my.company.web
。我在问题中添加了
应用程序类
。您真的需要使用
@EnableWebMvc
@EnableAutoConfiguration
?如果我没有弄错的话,它们已经包含在
@springbootcapplication
中,应该位于
应用程序
类中。这的确是事实,感谢您指出!然而,移除它并不能解决我的问题。你知道怎么做吗?你能展示一下你的整个
应用程序吗?另外,您的应用程序的包结构是什么(您将
应用程序放在哪个包中以及您将
网络配置放在哪里?
WebConfig
应用程序
都在同一个包中
my.company.web
。我将
应用程序类添加到问题中
。如果没有
@ComponentScan
,它之前在查找一些bean时遇到了问题。我在
应用程序
网络配置
类所在的位置添加了这个包,它可以正常工作!我以为它会自己扫描包裹。非常感谢你指出这一点!我会投票,但遗憾的是还不能。这正是我所想的。很高兴我能帮上忙。如果没有
@ComponentScan
,它之前在查找一些bean时遇到了问题。我在
应用程序
网络配置
类所在的位置添加了这个包,它可以正常工作!我以为它会自己扫描包裹。非常感谢你指出这一点!我会投票,但遗憾的是还不能。这正是我所想的。很高兴我能帮忙。
@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = UserRepository.class)
@ComponentScan(basePackages = { "my.company.controller", "my.company.Services", "java.lang.String","my.company.Services.Security" })
@EnableConfigurationProperties(my.company.Services.Storage.StorageProperties.class)
public class Application extends SpringBootServletInitializer {

@Autowired
private UserRepository repository;

@Autowired
private SecUserDetailsService userDetailService;


public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}

@Bean
CommandLineRunner init(StorageService storageService) {
    return (args) -> {

        repository.deleteAll();

        userDetailService.addUser("bob", "ross", "admin");
        userDetailService.addUser("1", "1", "superuser");
        userDetailService.addUser("2", "2", "admin");

        System.out.println("All users currently in DB:");
        System.out.println("-------------------------------");
        for (User user1 : repository.findAll()) {
            System.out.println(user1);
        }
        System.out.println();

        // storageService.deleteAll();

        try {
            storageService.init();
        } catch (StorageException e) {
            System.out.println("Ordner schon vorhanden");
        }
    };

//If i add this here french gets picked as default language, changing does still not work
@Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.FRENCH);
        return slr;
    }

}