4、html 页面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>Signin Template for Bootstrap</title> <!-- Bootstrap core CSS --> <link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.1.0/css/bootstrap.min.css}" rel="stylesheet"> <!-- Custom styles for this template --> <link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet"> </head> <body class="text-center"> <form class="form-signin" action="dashboard.html"> <img class="mb-4" src="asserts/img/bootstrap-solid.svg" th:src="@{/asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72"> <h1 class="h3 mb-3 font-weight-normal" th:text="#{index.tip}">Please sign in</h1> <label class="sr-only" th:text="#{index.username}">Username</label> <input type="text" class="form-control" placeholder="Username" th:placeholder="#{index.username}" required="" autofocus=""> <label class="sr-only" th:text="#{index.password}">Password</label> <input type="password" class="form-control" placeholder="Password" th:placeholder="#{index.password}" required=""> <div class="checkbox mb-3"> <label> <input type="checkbox" value="remember-me"> [[#{index.remember}]] </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{index.btn}">Sign in</button> <p class="mt-5 mb-3 text-muted">© 2017-2018</p> <a class="btn btn-sm" th:href="@{/index.html(lang='zh_CN')}">中文</a> <a class="btn btn-sm" th:href="@{/index.html(lang='en_US')}">English</a> </form> </body> </html>5、点击“中文”、“English”切换
原理:国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);
<a class="btn btn-sm" th:href="@{/index.html(lang='zh_CN')}">中文</a> <a class="btn btn-sm" th:href="@{/index.html(lang='en_US')}">English</a> import org.springframework.web.servlet.LocaleResolver; import org.thymeleaf.util.StringUtils; /** * 可以在连接上携带区域信息 * @author john * */ public class MyLocaleResolver implements LocaleResolver { /** * 解析区域信息 */ @Override public Locale resolveLocale(HttpServletRequest request) { String parameter = request.getParameter("lang"); // Locale.getDefault() 获取系统默认的 Locale locale = Locale.getDefault(); if(!StringUtils.isEmpty(parameter)){ String[] split = parameter.split("_"); locale = new Locale(split[0], split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { // TODO Auto-generated method stub } } import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import com.demo.springbootweb.component.MyLocaleResolver; // 使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能 // @EnableWebMvc 不要接管SpringMVC @Configuration public class MyMvcConfig extends WebMvcConfigurationSupport { /** * 配置自己的国际化语言解析器 * @return */ @Bean public LocaleResolver localeResolver() { return new MyLocaleResolver(); } } 感谢--尚硅谷