SpringBoot超詳細講解Thymeleaf模板引擎
Jsp是最早的模板技術,用來處理視圖層的,用來做數(shù)據(jù)顯示的模板

B S結構:
B:瀏覽器:用來顯示數(shù)據(jù),發(fā)送請求,沒有處理能力
發(fā)送一個請求,訪問a.jsp,a.jsp在服務器端變成Servlet,在將輸出的數(shù)據(jù)返回給瀏覽器,瀏覽器就可以看到結果數(shù)據(jù),jsp最終翻譯過來也是個html頁面
模板技術你就可以把它們當成字符串的替換,比如說:這里{data}這里有一個字符串,你把它換成固定值其他值,但是這個替換有一些附加的功能,通過模板技術處理視圖層的內(nèi)容

第一個例子:

pom.xml:Thymeleaf依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bjpowernode</groupId>
<artifactId>027-thymeleaf-first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--模板引擎起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--web起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>創(chuàng)建Controller控制器:HelloThymeleafController:
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
@RequestMapping("/hello")
public String helloThymeleaf(HttpServletRequest request){
//添加數(shù)據(jù)到request作用域,模板引擎可以從request中獲取數(shù)據(jù)
request.setAttribute("data","歡迎使用Thymeleaf模板引擎");
//指定視圖 模板引擎使用的頁面(html)
//邏輯名稱
return "hello";
}
}templates:用來放模板用到的視圖文件的,模板引擎用到的模板到放在了template目錄之下:
創(chuàng)建hello.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello html</title>
</head>
<body>
<h3>使用Thymeleaf的例子</h3>
<!--使用模板th:text=""獲取數(shù)據(jù)-->
<p th:text="${data}">想顯示數(shù)據(jù)</p>
</body>
</html>運行主啟動類Application:
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}可以在hello.html中加入:未解決在標簽中th爆紅,和寫的時候沒有提示信息
xmlns:th="http://www.thymeleaf.org" 在標簽中,再次寫th的時候就有提示記錄了
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>hello html</title>
</head>
<body>
<h3>使用Thymeleaf的例子</h3>
<!--使用模板th:text="${data}"獲取后端request的作用域中的數(shù)據(jù),把data數(shù)據(jù)替換文本,text表示取數(shù)據(jù)-->
<p th:text="${data}">想顯示數(shù)據(jù)</p>
<p th:text="${data}">顯示</p>
</body>
</html>
使用Model:
在Controller:
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
@RequestMapping("/hello")
public String helloThymeleaf(Model model, HttpServletRequest request){
//添加數(shù)據(jù)到request作用域,模板引擎可以從request中獲取數(shù)據(jù)
request.setAttribute("data","歡迎使用Thymeleaf模板引擎");
//使用model和request作用域是一樣的 實際上model中的數(shù)據(jù)就是放到request作用域中的
model.addAttribute("mydata","model中的數(shù)據(jù)");
//指定視圖 模板引擎使用的頁面(html)
//邏輯名稱
return "hello";
}
}hello.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>hello html</title>
</head>
<body>
<h3>使用Thymeleaf的例子</h3>
<!--使用模板th:text="${data}"獲取后端request的作用域中的數(shù)據(jù),把data數(shù)據(jù)替換文本,text表示取數(shù)據(jù)-->
<p th:text="${data}">想顯示數(shù)據(jù)</p>
<p th:text="${mydata}">顯示</p>
</body>
</html>
speingboot配置文件application.properties:模板引擎的常用相關設置,基本不用設置都是默認的:
#在開發(fā)階段,關閉模板發(fā)緩存,讓修改立即生效 設置為true時,就是使用模板緩存,當?shù)诙卧L問的時候,使用內(nèi)存中的數(shù)據(jù),就不在解析模板了
spring.thymeleaf.cache=false
#編碼格式
spring.thymeleaf.encoding=UTF-8
#模板的類型(默認是 html,模板是html文件 它不僅支持網(wǎng)頁做模板還支持其他類別的)
spring.thymeleaf.model=HTML
#模板的前綴:默認是類路徑的classpath:/templates目錄下
spring.thymeleaf.prefix=classpath:/templates/
#后綴
spring.thymeleaf.suffix=.html
到此這篇關于SpringBoot超詳細講解Thymeleaf模板引擎的文章就介紹到這了,更多相關SpringBoot Thymeleaf模板引擎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring-cloud-gateway降級的實現(xiàn)
這篇文章主要介紹了spring-cloud-gateway降級的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
EventBus與Spring Event區(qū)別詳解(EventBus 事件機制,Spring Event事件機制)
這篇文章主要介紹了EventBus與Spring Event區(qū)別,需要的朋友可以參考下2020-02-02
Spring Boot + FreeMarker 實現(xiàn)動態(tài)Word文檔導出功能
Spring Boot與FreeMarker的組合,為開發(fā)者提供了一個強大的平臺,可以輕松實現(xiàn)動態(tài)Word文檔的導出,本文將指導你如何使用Spring Boot與FreeMarker模板引擎,創(chuàng)建一個簡單的應用,用于根據(jù)數(shù)據(jù)庫數(shù)據(jù)動態(tài)生成Word文檔并下載,感興趣的朋友一起看看吧2024-06-06
解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式)
這篇文章主要介紹了解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
SpringBoot集成PDFBox實現(xiàn)電子簽章的代碼詳解
Apache PDFBox 是一個開源的 Java 庫,用于處理 PDF 文檔,它提供了一系列強大的功能,包括創(chuàng)建、渲染、拆分、合并、加密、解密 PDF 文件,以及從 PDF 中提取文本和元數(shù)據(jù)等,本文給大家介紹了SpringBoot集成PDFBox實現(xiàn)電子簽章,需要的朋友可以參考下2024-09-09

