1、导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.peng</groupId>
<artifactId>language</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>language</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、下载bootstrap模板
下载地址:https://getbootstrap.com/docs/5.0/examples/
3、导入模板到项目中
解压下载的模板:
复制css、js、img到static
文件夹
复制静态页面到templates
文件夹
4、创建自定义mvc配置类
//如果你想diy一些定制化的功能,只要写这个配置类,然后将它交给springboot,springboot就会帮我们自动装配
//扩展 MVC,需要实现 WebMvcConfigurer接口
@Configuration
//@EnableWebMvc 此注解标注此类将全面接管MVC,不能开启,否则mvc的自动配置全部失效
public class MyMvcConfig implements WebMvcConfigurer {
//重写试图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//首页跳转控制
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
//其他页面跳转
}
//ViewResolver实现了视图解析器的接口,我们就可以把它看作视图解析器
@Bean
public ViewResolver getViewResolver() {
return new MyViewResolver();
}
5、创建controller类
@Controller
public class IndexController {
@RequestMapping("/index")
private String Index(){
return "index";
}
}
6、创建国际化配置文件在resources文件夹下
下面的可以使用窗口化配置更简单
login.properties
login.password=密码
login.tip=请登录
login.username=用户名
login.btn=登录
login.remember=记住我
login_en_US.properties
login.password=password
login.tip=Please sign in
login.username=username
login.btn=sign in
login.remember=Remember me
login_zh_CN.properties
login.password=密码
login.tip=请登录
login.username=用户名
login.btn=登录
login.remember=记住我
7、创建国际化语言配置类
//国际化语言配置
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String language = request.getParameter("language");
Locale locale = Locale.getDefault();
if (StringUtils.hasLength(language)){
String[] split = language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
8、修改MVC配置类,加入以下代码
//国际化配置
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
9、配置application.properties文件
#springboot多环境配置
server.port=8080
#关闭模板缓存
spring.thymeleaf.cache=false
#国际化
spring.messages.basename=i18n.login
#没这个不生效
#时间日期格式化
spring.mvc.format.date=yyyy-MM-dd
10、修改index.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 th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/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>
<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">[[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.sign}]]</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</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>
这里有两种方式两种国际化方式