0.寫在前面:我這里還是使用的經(jīng)典教程(雷豐陽的課)里登錄頁面的例子所踊, 不同的是我使用的是SpringBoot2.3.0版本泌枪,寫法大同小異,稍有差別秕岛。
1.編寫國際化配置文件碌燕,注意文件的位置。
login.btn=登錄
login.password=密碼
login.remember=記住我
login.tip=請(qǐng)登錄
login.username=用戶名
------
login.btn=Sign In
login.password=password
login.remember=remember me
login.tip=please sign in
login.username=userName
2.去改寫login.html頁面中的內(nèi)容瓣蛀,主要是使用thymeleaf去改寫:
<!DOCTYPE html>
<!--引用 xmlns:th="http://www.thymeleaf.org"-->
<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 的 css,使用 th:href -->
<link th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<!-- 引用自己的css內(nèi)容 -->
<link th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
<!-- 引入國際化 h1標(biāo)簽陆蟆,這里用th:text"#{}"的形式 -->
<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標(biāo)簽,這里用th:placeholder"#{}"的形式 -->
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required=""
autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<!-- 引入國際化 行內(nèi)表達(dá)式惋增,注意這里寫法不同了哦 -->
<input type="checkbox" value="remember-me">[[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">? 2017-2018</p>
<a class="btn btn-sm" >中文</a>
<a class="btn btn-sm" >English</a>
</form>
</body>
</html>
效果這里就不貼圖了叠殷,畢竟沒啥難的,照做就是了诈皿,接下來處理根據(jù)我們的按鈕進(jìn)行中英文切換
1.改寫我們兩個(gè)a標(biāo)簽里的寫法:
<a class="btn btn-sm" th:href="@{/index.html(lan='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(lan='en_US')}">English</a>
2.定義我們自己的LocaleResolver,寫一個(gè)MyLocaleResolver
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String lan = httpServletRequest.getParameter("lan");
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(lan)) {
String[] split = lan.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
注意這里L(fēng)ocaleResolver 是引入的org.springframework.web.servlet包下的林束,不然也寫不出這個(gè)文件(這句話其實(shí)有些多余,純屬湊字)
3.在我們的MyConfig中稽亏,把MyLocaleResolver添加一下壶冒,不然自動(dòng)配置是掃描不到這個(gè)內(nèi)容的
@Configuration //標(biāo)注這是一個(gè)配置類
//使用WebMvcConfigurer 可以擴(kuò)展SpringMvc的方法
public class MyConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//設(shè)置一個(gè)視圖映射
registry.addViewController("/testView").setViewName("success");
//http://localhost:8099/testView 會(huì)自動(dòng)重定向到 http://localhost:8099/success
//既保留了自動(dòng)配置,也能用我們擴(kuò)展的配置
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
}
//添加 MyLocaleResolver
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
}
這里MyConfig是實(shí)現(xiàn)了WebMvcConfigurer 接口截歉,在SpringBoot 2.0以前版本好像是繼承WebMvcConfigurationAdapter類來實(shí)現(xiàn)類似功能的胖腾,感興趣的可以去找找看,視頻教程就是瘪松。本人就不貼那個(gè)例子的寫法了咸作,畢竟不喜歡開歷史的倒車。
這些完了就可以測試了宵睦,不貼結(jié)果了记罚,畢竟這是一篇學(xué)習(xí)筆記,很水的文壳嚎。