springboot集成thymeleaf(入門篇)

一 前言

本篇內容是關於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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章