溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot中如何使用Thymeleaf操作

發布時間:2022-03-04 12:08:17 來源:億速云 閱讀:225 作者:iii 欄目:web開發

這篇“SpringBoot中如何使用Thymeleaf操作”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“SpringBoot中如何使用Thymeleaf操作”文章吧。

前言

Thymeleaf是一個適用于Web和獨立環境的現代服務器端Java模板引擎。

Thymeleaf的主要目標是為您的開發工作流程帶來優雅的自然模板 - 可以在瀏覽器中正確顯示的HTML,也可以用作靜態原型,從而在開發團隊中實現更強大的協作。

通過Spring Framework模塊,與您喜歡的工具的大量集成,以及插入您自己的功能的能力,Thymeleaf是現代HTML5 JVM Web開發的理想選擇

Thymeleaf與Velocity或FreeMarker等其他模板引擎相比如何?

Velocity 和 FreeMarker 都是非常棒的軟件,但是它們處理模板問題的原理與Thymeleaf 完全不同。

Thymeleaf 非常強調自然模板化——允許模板成為工作原型,而其他兩個模板不允許這樣做——它的語法試圖(可以說)更干凈,更符合當前 web開發的趨勢。另外,從架構的角度來看,Velocity 和 FreeMarker 都是順序文本處理器,而 Thymeleaf 是基于標記解析技術的。這允許 Thymeleaf 利用特定于基于標記的環境的有趣特性,特別是web。

SpringBoot使用Thymeleaf 操作步驟

第一步是引入Thymeleaf starter依賴,具體代碼如下:

<!-- thymeleaf 相關依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

第二步是在 application.properties 添加 Thymeleaf 相關配置,具體配置如下:

server.port=8090
server.servlet.context-path=/sbe

#關閉 Thymeleaf 的緩存開發過程中無需重啟
spring.thymeleaf.cache = false
#設置thymeleaf頁面的編碼
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
#設置thymeleaf頁面的后綴
spring.thymeleaf.suffix=.html
#設置thymeleaf頁面的存儲路徑
spring.thymeleaf.prefix=classpath:/templates/

第三步是編寫訪問 Thymeleaf 頁面 Controller。

@Controller
@RequestMapping("/hello")
public class ThymeleafHelloWrodController {
    
    @RequestMapping("/thymeleaf")
    public String helloThymeleaf(Model model){
        model.addAttribute("hello","hello Thymeleaf!");
        return "hello/index";
    }
}

Thymeleaf 頁面代碼如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <p th:text="${hello}">hello</p>
</body>
</html>

測試

在游覽器輸入訪問 Thymeleaf 頁面的 Controller 的 URL:http://localhost:8090/sbe/hello/thymeleaf 進行測試,測試結果如下:
SpringBoot中如何使用Thymeleaf操作

以上就是關于“SpringBoot中如何使用Thymeleaf操作”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女