詳解spring Boot 集成 Thymeleaf模板引擎實(shí)例
今天學(xué)習(xí)了spring boot 集成Thymeleaf模板引擎。發(fā)現(xiàn)Thymeleaf功能確實(shí)很強(qiáng)大。記錄于此,供自己以后使用。
Thymeleaf:
- Thymeleaf是一個(gè)java類庫,他是一個(gè)xml/xhtml/html5的模板引擎,可以作為mvc的web應(yīng)用的view層。
- Thymeleaf還提供了額外的模塊與Spring MVC集成,所以我們可以使用Thymeleaf完全替代jsp。
spring Boot
- 通過org.springframework.boot.autoconfigure.thymeleaf包對(duì)Thymeleaf進(jìn)行了自動(dòng)配置。
- 通過ThymeleafAutoConfiguration類對(duì)集成所需要的bean進(jìn)行自動(dòng)配置。包括templateResolver,templateEngine,thymeleafViewResolver的配置。
下面我將演示spring boot 日常工作中常用的Thymeleaf用法。
Spring Boot 日常工作中常用Thymeleaf的用法
1:首先,在創(chuàng)建項(xiàng)目的時(shí)候選擇依賴中選中Thymeleaf,或者在pom中添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
或者項(xiàng)目名-右鍵-add Framework Support來添加依賴jar包。如圖
2:示例javaBean
此類用來在模板頁面展示數(shù)據(jù)用。包含name和age屬性。
public class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
3.腳本樣式靜態(tài)文件
根據(jù)默認(rèn)原則,腳本樣式,圖片等靜態(tài)文件應(yīng)放置在src/main/resources/static下,這里引入了Bootstrap和jQuery,結(jié)構(gòu)如圖所示:
4.演示頁面
根據(jù)默認(rèn)原則,頁面應(yīng)放置在src/main/resources/templates下。在src/main/resources/templates下面新建index.html,如上圖。
代碼如下:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta name="viewport" content="width=device-width,initial-scale=1"/> <link th:href="@{bootstrap/css/bootstrap.min.css}" rel="external nofollow" rel="stylesheet"/> <link th:href="@{bootstrap/css/bootstrap-theme.min.css}" rel="external nofollow" rel="stylesheet"/> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">訪問model</h3> </div> <div class="panel-body"> <span th:text="${singlePerson.name}"></span> </div> <div th:if="${not #lists.isEmpty(people)}"> <div class="panel panel-primary"> <h3 class="panel-title">列表</h3> </div> <div class="panel-body"> <ul class="panel-group"> <li class="list-group-item" th:each="person:${people}"> <span th:text="${person.name}"></span> <span th:text="${person.age}"></span> <button class="btn" th:onclick="'getName(\''+${person.name}+'\')'">獲得名字</button> </li> </ul> </div> </div> </div> <script th:src="@{jquery-1.10.2.min.js}" type="text/javascript"></script> <script th:src="@{bootstrap/js/bootstrap.min.js}"></script> <script th:inline="javascript"> var single=[[${singlePerson}]]; console.log(single.name+"/"+single.age); function getName(name) { console.log(name); } </script> </body> </html>
5.數(shù)據(jù)準(zhǔn)備
代碼如下:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @Controller @SpringBootApplication public class ThymeleafTestApplication { @RequestMapping("/") public String index(Model model){ Person single=new Person("aa",1); List<Person> people=new ArrayList<Person>(); Person p1=new Person("bb",2); Person p2=new Person("cc",3); Person p3=new Person("dd",4); people.add(p1); people.add(p2); people.add(p3); model.addAttribute("singlePerson",single); model.addAttribute("people",people); return "index"; } public static void main(String[] args) { SpringApplication.run(ThymeleafTestApplication.class, args); } }
6.運(yùn)行
訪問http://localhost:8080效果如圖:
單擊“獲得名字” f12產(chǎn)看頁面控制臺(tái)打印的日志效果如圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 如何讀取Excel格式xls、xlsx數(shù)據(jù)工具類
這篇文章主要介紹了Java 如何讀取Excel格式xls、xlsx數(shù)據(jù)工具類的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java實(shí)現(xiàn)檢測(cè)是否字符串中包含中文
本文給大家分享了2個(gè)使用java檢測(cè)字符串中是否包含中文的代碼,都非常的實(shí)用,最后附上了各種字符的unicode編碼的范圍,方便我們以后使用正則進(jìn)行匹配檢測(cè)。2015-10-10Java在PowerPoint幻燈片中創(chuàng)建散點(diǎn)圖的方法
散點(diǎn)圖是通過兩組數(shù)據(jù)構(gòu)成多個(gè)坐標(biāo)點(diǎn),考察坐標(biāo)點(diǎn)的分布,判斷兩變量之間是否存在某種關(guān)聯(lián)或總結(jié)坐標(biāo)點(diǎn)的分布模式,這篇文章主要介紹了Java如何在PowerPoint幻燈片中創(chuàng)建散點(diǎn)圖,需要的朋友可以參考下2023-04-04idea 在springboot中使用lombok插件的方法
這篇文章主要介紹了idea 在springboot中使用lombok的相關(guān)資料,通過代碼給大家介紹在pom.xml中引入依賴的方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08解決springboot項(xiàng)目上傳文件出現(xiàn)臨時(shí)文件目錄為空的問題
這篇文章主要介紹了解決springboot項(xiàng)目上傳文件出現(xiàn)臨時(shí)文件目錄為空的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java數(shù)據(jù)結(jié)構(gòu)之雙向鏈表的實(shí)現(xiàn)
相較單鏈表,雙向鏈表除了data與next域,還多了一個(gè)pre域用于表示每個(gè)節(jié)點(diǎn)的前一個(gè)元素。這樣做給雙向鏈表帶來了很多優(yōu)勢(shì)。本文主要介紹了雙向鏈表的實(shí)現(xiàn),需要的可以參考一下2022-10-10Java中鎖的實(shí)現(xiàn)和內(nèi)存語義淺析
這篇文章主要給大家介紹了關(guān)于Java中鎖的實(shí)現(xiàn)和內(nèi)存語義的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11