【发布时间】:2017-08-04 12:53:00
【问题描述】:
我目前正在开发一个使用 Spring Boot(包括 Spring Security)和 thymeleaf 的 Web 应用程序。目前,我正在尝试整合对英语和德语语言的国际化支持作为开始。
对于基础知识,我遵循 this 教程并尝试让他们的示例正常工作。 现在,如果我转到 Localhost:8443/international 并选择其中一种语言,则 URL 将正确构建为 .../international?lang=en。 Thymeleaf 甚至读取 .propperties 文件中标记为默认的字段。但无论我做什么,我都无法让它真正切换语言。
代码:
@Configuration
@EnableWebMvc
@EnableAutoConfiguration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
}
像这样我假设它采用默认的messages.propperties。但是,如果我将 LocaleResolver Bean 放入我的
public class Application extends SpringBootServletInitializer
主要方法所在的类,它采用在那里设置为默认语言环境的任何语言。
从我现在所处的位置,我得出结论,我的 .propperties 文件很好并且可以读取,但是使用 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;
}
}
【问题讨论】:
-
你真的需要使用
@EnableWebMvc和@EnableAutoConfiguration吗?如果我没记错的话,它们已经包含在@SpringBootApplication中,它应该位于您的Application类中。 -
确实是这样,谢谢指出!然而,删除它并不能解决我的问题。你知道怎么做吗?
-
你能展示你的整个
Application类吗?还有你的应用程序的包结构是什么(你把Application放在哪个包里,WebConfig放在哪里)? -
WebConfig和Application在同一个包my.company.web中。我添加了Application class into the question。
标签: java spring spring-mvc spring-boot internationalization