SpringBoot超詳細講解Thymeleaf模板引擎
Jsp是最早的模板技術(shù),用來處理視圖層的,用來做數(shù)據(jù)顯示的模板
B S結(jié)構(gòu):
B:瀏覽器:用來顯示數(shù)據(jù),發(fā)送請求,沒有處理能力
發(fā)送一個請求,訪問a.jsp,a.jsp在服務(wù)器端變成Servlet,在將輸出的數(shù)據(jù)返回給瀏覽器,瀏覽器就可以看到結(jié)果數(shù)據(jù),jsp最終翻譯過來也是個html頁面
模板技術(shù)你就可以把它們當成字符串的替換,比如說:這里{data}這里有一個字符串,你把它換成固定值其他值,但是這個替換有一些附加的功能,通過模板技術(shù)處理視圖層的內(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:模板引擎的常用相關(guān)設(shè)置,基本不用設(shè)置都是默認的:
#在開發(fā)階段,關(guān)閉模板發(fā)緩存,讓修改立即生效 設(shè)置為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
到此這篇關(guān)于SpringBoot超詳細講解Thymeleaf模板引擎的文章就介紹到這了,更多相關(guān)SpringBoot Thymeleaf模板引擎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot如何從數(shù)據(jù)庫獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化)
這篇文章主要介紹了springboot如何從數(shù)據(jù)庫獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12netty-grpc一次DirectByteBuffer內(nèi)存泄露問題
這篇文章主要介紹了netty-grpc一次DirectByteBuffer內(nèi)存泄露問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12javaweb中Filter(過濾器)的常見應(yīng)用
這篇文章主要介紹了javaweb中Filter的常見應(yīng)用,過濾器的使用方法,感興趣的小伙伴們可以參考一下2015-12-12