Springboot 国际化语言报404

复写resolveLocale方法,按照指定格式,获取语言

public class MylocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            String[] s = l.split("_");
             locale = new Locale(s[0],s[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

实现WebMvcConfigurer
第一步重定向index.html到login.html
第二步使用我们定义的LocaleResolver 方法,
注意的是方法名必须为localeResolver

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
            }
        };
        return webMvcConfigurer;
    }

    @Bean
    public LocaleResolver localeResolver(){
        return new MylocaleResolver();
    }

}

在login.html中@{/index.html(l=‘zh_CN’)},这里注意写的是index.html,我开始写的是login.html,
注意:login.html不能直接进去,只有通过重定向的方式才可以,404报错就因为这个原因

    <a href="#" class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文a>
    <a href="#" class="btn btn-sm" th:href="@{/index.html(l='en_US')}">Englisha>

你可能感兴趣的:(Springboot)