一 前言
本篇內容是關於thymeleaf的入門知識,即thymeleaf的引擎模板的入門搭建過程;
公眾號:知識追尋者
知識追尋者(Inheriting the spirit of open source, Spreading technology knowledge;)
二 springboot整合thymeleaf
2.1 thymeleaf簡介
Thymeleaf 是 Java 模板引擎,Spring 官方推薦使用,也是 Spring Boot 默認的模板引擎;前后端分離之前就是thymeleaf這類引擎模板的地盤;其支持HTML5的視圖模板,能夠無縫銜接springboot;主要用途能進行web開發和非web開發,比如頁面渲染,代碼生成,文檔生成等等,做些日常的小工具是個很好的選擇;
2.2 pom.xml
springboot 2.x; maven 3.5 ; jdk -1.8; 引入 web 和 thymeleaf 啟動器;
<!--thymeleaf引擎模板-->
<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>
<!-- html無需嚴格校驗-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.15</version>
</dependency>
2.3 application.yml
thymeleaf 引擎模板的相關配置,主要是字符集, 資源路徑,模板路徑,關閉緩存;
server:
port: 9200
spring:
# 引擎模板配置
thymeleaf:
cache: false # 關閉緩存
mode: LEGACYHTML5 # 去除htm5嚴格校驗
prefix: classpath:/templates/ # 指定 thymeleaf 模板路徑
encoding: UTF-8 # 指定字符集編碼
mvc:
static-path-pattern: /static/** # js ,css 等靜態文件路徑
2.3 實體
用戶實體存儲數據,實現內容就是在控制層進行封裝數據通過springMVC的 ModelAndView 進行視圖轉發至thymeleaf 引擎模板位置;
public class User {
private Long id;
private String name;
private Integer age;
}
2.4 控制層
學過springMVC的讀者應該不陌生,非常容易理解,就是 請求, 封裝數據,視圖轉發,渲染;
/**
* @Author lsc
* <p> </p>
*/
@Controller//聲明一個控制器
public class UserController {
@GetMapping("/user")// 請求路徑為 ip + port + /user
// 當訪問此路徑時會轉發至邏輯視圖 user
public String getUser(Model model){
// list中存放 用戶
ArrayList<User> userList = new ArrayList<>();
for (long i=0; i<5; i++){
User user = new User();
user.setId(i);
user.setAge(18);
user.setName("知識追尋者");
userList.add(user);
}
// 為視圖添加用戶
model.addAttribute("users",userList);
// 邏輯視圖為 user 即在 templates 下的 user.html
return "user";
}
}
2.5 user.html
user.html 文件位置為resource/templates/ 下;語法知識看文末鏈接;
<!DOCTYPE html>
<!--將默認的頭 <html lang="en"> 引入thymeleaf-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用戶頁面</title>
</head>
<body>
<table border="1px">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<!--通過 th:aech 語法迭代user集合-->
<tr th:each="user: ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
</body>
</html>
2.6 啟動結果
啟動工程 訪問路徑 http://localhost:9200/user
頁面內容如下;
id name age
0 知識追尋者 18
1 知識追尋者 18
2 知識追尋者 18
3 知識追尋者 18
4 知識追尋者 18
三 進階知識
對於深入學習引擎模板的語法相關知識,給出鏈接如下
官網 : https://www.thymeleaf.org/documentation.html
github : https://github.com/thymeleaf
相關技術博客:
https://segmentfault.com/a/1190000016284066
https://www.jianshu.com/p/d24436f6cb70
本套教程
- springboot入門 (1)
- Springboot自定義banner(2)
- springboot配置文件解析(3)
- springboot集成mybatis(4)
- springboot集成jdbcTemplate(5)
- spingboot單元測試(6)
- springboot集成thymeleaf(7)
- springboot多文件上傳(8)
- springboot文件下載(9)
- Springboot自定義異常類(10)
- springboot多環境配置(11)
- springboot自動配置原理解析(12)
- springboot集成restTemplate接口調用(13)
- springboot集成任務調度(14)
- springboot跨域CORS處理(15)
- springboot開啟GIZP壓縮(16)
- springboot集成logback(17)
- springboot集成Swagger(18)
- springboot集成actuator后台監控(19)
- springboot集成mybatis+oracle+druid(20)
- springboot 集成springsession(21)
- springboot集成jwt(22)
- springboot集成admin后台監控(23)
- springboot集成redis基礎篇(24)
- springboot集成redis緩存篇(25)
- springboot使用AOP日志攔截(26)
- springboot集成Validation參數校驗(27)
- springboot集成mybatisPlus(28)
- springboot集成shiro(29)
- springboot實現接口等冪次校驗(30)
- springboot-集成WebSockets(31)
- restTemplate源碼解析(32)
- SpringBoot使用@Async異步調用與線程池(33)
- 待續