查看jar包的依賴
https://www.webjars.com/
Thymeleaf 文檔
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
國際化
- 創建配置文件 login_en_US.properties login_zh_CN.properties
login.remember=Remember Me
login.password=Password
login.tip=Please sign in
login.username=UserName
login.btn=sign In
- 創建自己的LocaleResolver
//local 從URL中獲取或者用系統默認的
//http://localhost:8080/index.html?&l=en_US
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[] split = l.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
- 加入到容器中 在MyMvcConfig
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
registry.addViewController("/panwang").setViewName("success");
}
@Bean
public WebMvcConfigurerAdapter webMvcAutoConfigurationAdapter(){
WebMvcConfigurerAdapter adpter = new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
registry.addViewController("index.html").setViewName("index");
}
};
return adpter;
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
- 頁面修改 thymeleaf 屬性來調用對應的語言
<!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" rel="stylesheet">
<!-- Custom styles for this template -->
<link 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" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<label class="sr-only" th:text="#{login.username}">Username</label>
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only">Password</label>
<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me" th:text="#{login.remember}"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.tip}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2020</p>
<a class="btn btn-sm" th:href="@{/index.html?(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html?(l='en_US')}">English</a>
</form>
</body>
</html>