1、访问 index.html 路径:1)、http://localhost:8080/crud/ 2)、http://localhost:8080/crud/index 3)、http://localhost:8080/crud/index.html |
2、国际化的操作 1、编写国际化的配置文件 2、使用 ResourceBundleMessageSource 管理国际化资源文件 3、在页面使用 fmt:message 取出国际化内容 上面操作是 Spring 中的操作,以下是在 SpringBoot 中的操作 1、编写国际化的配置文件 |
3、在 application.properties 中配置相关信息
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(); } }
感谢--尚硅谷 |