SpringBoot——》thymeleaf
一、項目依賴配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、thymeleaf參數配置
參數對應jar包(版本可變動):spring-boot-autoconfigure-2.1.1.RELEASE.jar
參數對應java類:org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
注:在 application.yml
或 application.property
中配置
spring:
# 模板引擎
thymeleaf:
# Thymeleaf3.0模板:HTML (默認)、XML、TEXT、JAVASCRIPT、CSS、RAW
mode: HTML
encoding: utf-8
# 禁用緩存:避免改了模板還要重啓服務器
cache: false
# 資源信息
messages:
# 國際化資源文件路徑
basename: static/i18n/messages
1、默認模板存放路徑
默認值:classpath:/templates/
可修改:
spring:
thymeleaf:
prefix:/templates/
2、默認模板後綴
默認值:.html
可修改:
spring:
thymeleaf:
suffix:.html
3、默認靜態資源路徑 (4種)
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
三、示例:Controller和index.html
在src/main/java/com/controller
下新建 WebController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class WebController {
@GetMapping("/index")
public String index(){
return "index";
}
}
在 classpath:/resources/templates
下新建 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>我是小仙</h1>
</body>
</html>