在按钮单击Java Spring Boot上更改语言环境

时间:2019-06-09 09:53:00

标签: java spring spring-boot

我是Java Spring Boot的新手,我的问题是我在配置文件中设置了语言环境,并且我想在登录页面的按钮单击上更改语言环境,所以我一直尝试不成功。希望有人帮助我实现这一目标

我的配置文件:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Bean
public BCryptPasswordEncoder passwordEncoder() {
    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
    return bCryptPasswordEncoder;
}


  @Bean
  public LocaleResolver localeResolver() {
      SessionLocaleResolver localeResolver = new SessionLocaleResolver();
      localeResolver.setDefaultLocale(new Locale("cs","CZ")); // change this
      return localeResolver;
  }

    @Bean(name = "messageSource")
    public MessageSource getMessageResource()  {
        ReloadableResourceBundleMessageSource messageResource= new ReloadableResourceBundleMessageSource();

        // Read i18n/messages_xxx.properties file.
        // For example: i18n/messages_en.properties
        messageResource.setBasename("classpath:i18n/messages");
        messageResource.setDefaultEncoding("UTF-8");
        return messageResource;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
        localeInterceptor.setParamName("lang");
         registry.addInterceptor(localeInterceptor).addPathPatterns("/*");
    }

}

我的控制器:

@RequestMapping(value = {"/login" }, method = RequestMethod.GET)
public ModelAndView userLogin() {
//System.out.print("LAnguage"+lang);

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("login"); // resources/template/login.html
    //modelAndView.addObject("errorMsg", "Invalid Credential");
    return modelAndView;
 }

检查此一项: Spring Boot - Change Locale on click

0 个答案:

没有答案