Jsp是最早的模板技術,用來處理視圖層的,用來做資料顯示的模板
B S結構:
B:瀏覽器:用來顯示數據,發送請求,沒有處理能力
發送一個請求,訪問a.jsp,a.jsp在伺服器端變成Servlet,在將輸出的數據回傳給瀏覽器,瀏覽器就可以看到結果數據,jsp最後翻譯過來也是個html頁面
模板技術你就可以把它們當成字串的替換,比如說:這裡{data}這裡有一個字串,你把它換成固定值其他值,但是這個替換有一些附加的功能,透過模板技術處理視圖層的內容
第一個例子:
#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>
建立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){ //添加数据到request作用域,模板引擎可以从request中获取数据 request.setAttribute("data","欢迎使用Thymeleaf模板引擎"); //指定视图 模板引擎使用的页面(html) //逻辑名称 return "hello"; } }
templates:用來放模板用到的視圖檔的,模板引擎用到的模板到放在了template目錄之下:
創建hello.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hello html</title> </head> <body> <h4 id="使用Thymeleaf的例子">使用Thymeleaf的例子</h4> <!--使用模板th:text=""获取数据--> <p th:text="${data}">想显示数据</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> <h4 id="使用Thymeleaf的例子">使用Thymeleaf的例子</h4> <!--使用模板th:text="${data}"获取后端request的作用域中的数据,把data数据替换文本,text表示取数据--> <p th:text="${data}">想显示数据</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){ //添加数据到request作用域,模板引擎可以从request中获取数据 request.setAttribute("data","欢迎使用Thymeleaf模板引擎"); //使用model和request作用域是一样的 实际上model中的数据就是放到request作用域中的 model.addAttribute("mydata","model中的数据"); //指定视图 模板引擎使用的页面(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> <h4 id="使用Thymeleaf的例子">使用Thymeleaf的例子</h4> <!--使用模板th:text="${data}"获取后端request的作用域中的数据,把data数据替换文本,text表示取数据--> <p th:text="${data}">想显示数据</p> <p th:text="${mydata}">显示</p> </body> </html>
speingboot設定檔application.properties:模板引擎的常用相關設置,基本不用設定都是預設的:
#在開發階段,關閉範本發緩存,讓修改立即生效設定為true時,就是使用模板緩存,當第二次訪問的時候,使用內存中的數據,就不在解析模板了
spring.thymeleaf.cache=false
#編碼格式
spring.thymeleaf.encoding=UTF-8
#範本的類型(預設是html,範本是html檔案它不僅支援網頁做範本還支援其他類別的)
spring.thymeleaf.model= HTML
#範本的前綴:預設是類別路徑的classpath:/templates目錄下
spring.thymeleaf.prefix=classpath:/templates/
#後綴
spring.thymeleaf.suffix=.html
以上是SpringBoot之Thymeleaf模板引擎實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。