springBoot整合thymeleaf


Thymeleaf作為后端頁面開發模板,避免了前后端分離帶來的跨域等問題,對於后台管理等簡單頁面可以快速進行開發迭代。 學習Thymeleaf,首先了解一下如何將Thymeleaf集成到SpringBoot項目中使用。

1. 創建SpringBoot項目

1.1 選擇起步依賴

在創建SpringBoot項目時,官方提供了很多的起步依賴,其中就有Thymeleaf相關的啟動器,在Template Engines模板引擎下勾選Thymeleaf啟動器,由於是Web頁面的開發,所以還需要引入Web模塊啟動器。

image.png

勾選后點擊下一步並完成項目創建。

1.2 手動添加依賴

如果在項目創建時沒有選擇相關的Thymeleaf啟動器,則可以在maven的坐標配置pom.xml文件中添加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> 復制代碼

完成創建后,就得到了一個集成Thymeleaf的SpringBoot項目,結構上與普通項目一樣,但是在resources文件夾下多了一個templates文件夾,這個文件夾就是用於存放Thymeleaf模板文件。

image.png

2. Thymeleaf配置

2.1 SpringBoot自動配置Thymeleaf

SpringBoot框架中提供了對Thymeleaf模板引擎的自動加載配置,在SpringBoot的自動配置類包中,有一個org.springframework.boot.autoconfigure.thymeleaf包,定義的ThymeleafProperties類就是thymeleaf的自動配置類,其中指定了thymeleaf框架的一些默認屬性,比如模板的默認路徑前綴classpath:/templates/,以及模板文件格式后綴.html

ThymeleafProperties類的部分定義內容如下:

//自動配置類ThymeleafProperties @ConfigurationProperties( prefix = "spring.thymeleaf" ) public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; private boolean checkTemplate = true; private boolean checkTemplateLocation = true; private String prefix = "classpath:/templates/"; private String suffix = ".html"; private String mode = "HTML"; ... } 復制代碼

可以看出,配置類讀取的是SpringBoot配置文件中以spring.thymeleaf作為前綴的配置內容,因此,如果需要自定義thymeleaf的相關配置,只需要在application.properties文件中指定即可。

2.2 自定義配置項

自動配置類ThymeleafProperties初始化時會讀取application.properties文件中配置的內容,配置文件中可以定義的thymeleaf配置項有:

# thymeleaf模板文件前綴,可以自定義文件夾如classpath:/templates/temp spring.thymeleaf.prefix=classpath:/templates/ # thymeleaf模板文件后綴 spring.thymeleaf.suffix=.html # 視圖模板類型 spring.thymeleaf.mode=HTML # 默認視圖編碼格式 spring.thymeleaf.encoding=UTF-8 # 響應類型 spring.thymeleaf.servlet.content-type=text/html # 配置頁面緩存,thymeleaf默認開啟緩存,頁面不能及時刷新,需要關閉 spring.thymeleaf.cache=false 復制代碼

通過自定義的配置相關屬性的值,在實際使用時可以更好的控制對thymeleaf模板引擎的使用。

3. Thymeleaf頁面效果

創建了項目,添加了配置,下面就來看一下thymeleaf帶來的頁面效果。

3.1 定義HTML文件

使用thymeleaf模板引擎展示頁面,首先要創建一個html文件,並按照thymeleaf語法編碼與接口數據綁定。

  • 定義html文件時,要對其中的<html>標簽使用xmlns:th="http://www.thymeleaf.org"聲明,這樣在當前html頁面中才可以使用thymeleaf語法。
  • 對於服務接口綁定返回的數據集合,可以使用th:each="user : ${userEntityList}"遍歷對應值
  • 對於每個屬性的值,使用th:text="${user.getName()}"來獲取展示
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>姓名</th>
                <th>性別</th>
                <th>電話</th>
            </tr>
        </thead>
        <tbody>
        <tr th:each="user : ${userEntityList}">
            <td th:text="${user.getName()}">name</td>
            <td th:text="${user.getGender()}">male</td>
            <td th:text="${user.getPhone()}">13812341234</td>
        </tr>
        </tbody>
    </table>
</body>
</html>
復制代碼

3.2 定義服務接口

定義服務接口來向thymeleaf模板傳遞數據,接口定義時需要注意:

  • 使用@Controller注解而不是@RestController,因此@RestController會將結果解析后直接展示字符串內容,而不能根據字符串獲取對應的模板名稱
  • 服務接口需要加入Model對象作為參數,並使用Model對象來綁定需要傳遞的數據,以便在thymeleaf中使用
@Controller
public class ThymeleafController {
    @RequestMapping("/index")
    public String getIndex(Model model){
        List<UserEntity> userList = new ArrayList<>();
        UserEntity user = new UserEntity("tom","female", "17788996600");
        userList.add(user);
        model.addAttribute(userList);
        return "index";
    }
}

3.3 頁面展示

如此這般之后,可以運行項目,並請求定義的服務接口,此時會根據接口將數據加入Model中,並根據接口的返回結果找到對應的thymeleaf模板文件,在模板文件中將數據渲染,最終展示在頁面中。

 

 

在 Thymeleaf 之中邏輯運算可以使用下面的一些運算符來完成,例如:and、or、關系比較(>、<、>=、<=、==、!=、lt、gt、le、ge、eq、ne等)。

下面說明一下:lt、gt、le、ge、eq、ne所代表的含義:

lt:less than 小於
le:less than or equal to 小於等於
eq:equal to 等於
ne:not equal to 不等於
ge:greater than or equal to 大於等於
gt:greater than 大於
not: 非運算
mod:取模
and:與運算
or:或運算

 

 


轉載:https://juejin.cn/post/7030820165042307108


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM