SpringBoot使⽤Thymeleaf配置国际化页⾯原⽣⽬录:
1.在l⽂件中添加Thymeleaf相关依赖
<!--SpringBoot⽗项⽬依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--导⼊ spring-boot-starter-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.3.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--设置项⽬热部署——⾃动更新-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<!--Thymeleaf 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
使⽤插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--热部署配置-->
<configuration>
<!--fork:如果没有该项配置,整个devtools不会起作⽤-->            <fork>true</fork>
</configuration>
</plugin>
2.在resources中创建Springboot全局配置⽂件application.properties,⽤来配置
Thymeleaf模板的参数以及添加国际化⽂件的基础名。
#thymeleaf页⾯缓存设置(默认为true),开发中⽅便调试应设置为false,
#上线稳定后应保持默认true
spring.thymeleaf.cache=false
ding=UTF-8
#配置国际化⽂件基础名
3.在resources中创建i18n⽂件夹,编写多语⾔国际化⽂件。并在该⽬录下创建login.properties、login_zh_CN.properties、login_en_US.properties三个⽂件。
(1)login.properties
login.tip=请登录
login.username=⽤户名
login.password=密码
login.button=登录
(2)login_zh_CN.properties
login.tip=请登录
login.username=⽤户名
login.password=密码
login.button=登录
(3)login_en_US.properties
login.tip=Please sign in
login.username=Username
login.password=Password
login.button=Login
4.在ample下创建config包,在包下新建MyLocalResovel类,并且继承LocaleResolver。
import t.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
@Configuration
public class MyLocalResovel implements LocaleResolver {
//⾃定义区域解析器
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取页⾯⼿动切换传递的语⾔参数l
String l = Parameter("l");
thymeleaf用法//获取请求头⾃动传递的语⾔参数Accept-Language
String header = Header("Accept-Language");
Locale locale = null;
/*
* 如果⼿动切换参数不为空,
* 就根据⼿动参数进⾏语⾔切换,
* 否则默认根据请求头信息切换
*/
if(!StringUtils.isEmpty(l)){
String[] split = l.split("_");
locale = new Locale(split[0],split[1]);
}else{
//Accept-Language:en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
String[] splits=header.split(",");
String[] split=splits[0].split("-");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
}
//将⾃定义的MyLocalResovel类重新注册为⼀个类型LocaleResolver的Bean组件
@Bean
public MyLocalResovel localeResolver(){
return new MyLocalResovel();
}
}
5.创建controller包,并新建LoginController类作web控制器
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Calendar;
@Controller
public class LoginController {
/*
*获取并封装当前年份跳转到登录页login.html*parammodel
*@return
*/
@RequestMapping("/toLoginPage")
public String toLoginPage(Model model){
model.addAttribute("currentYear", Instance().get(Calendar.YEAR));
return "login";
}
}
6.创建模板页⾯login.html并引⼊静态资源⽂件bootstrap.min.css与signin.css
<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection-->
<html lang="en" xmlns:th="">
<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">
<title>⽤户登录界⾯</title>
<link th:href="@{/login/css/bootstrap.min.css}" rel="stylesheet">
<link th:href="@{/login/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<!--⽤户登录form表单-->
<form class="form-signin">
<img class="mb-4" th:src="@{/login/img/login.jpg}" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">请登录</h1>
<input type="text" class="form-control" th:placeholder="#{login.username}"required="" autofocus="">    <input type="password" class="form-control" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me">[[#{berme}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.button}">登录</button>    <p class="mt-5 mb-3 text-muted">
<span th:text="${currentYear}">2018</span>-<span th:text="${currentYear}+1">2019</span>
</p>
<a class="btn btn-sm" th:href="@{/toLoginPage(l='zh_CN')}">中⽂</a>
<a class="btn btn-sm" th:href="@{/toLoginPage(l='en_US')}">English</a>
</form>
</body>
</html>
7.启动类SpringbootApplication

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。