詳解spring Boot 集成 Thymeleaf模板引擎實(shí)例
今天學(xué)習(xí)了spring boot 集成Thymeleaf模板引擎。發(fā)現(xiàn)Thymeleaf功能確實(shí)很強(qiáng)大。記錄于此,供自己以后使用。
Thymeleaf:
- Thymeleaf是一個(gè)java類(lèi)庫(kù),他是一個(gè)xml/xhtml/html5的模板引擎,可以作為mvc的web應(yīng)用的view層。
- Thymeleaf還提供了額外的模塊與Spring MVC集成,所以我們可以使用Thymeleaf完全替代jsp。
spring Boot
- 通過(guò)org.springframework.boot.autoconfigure.thymeleaf包對(duì)Thymeleaf進(jìn)行了自動(dòng)配置。
- 通過(guò)ThymeleafAutoConfiguration類(lèi)對(duì)集成所需要的bean進(jìn)行自動(dòng)配置。包括templateResolver,templateEngine,thymeleafViewResolver的配置。
下面我將演示spring boot 日常工作中常用的Thymeleaf用法。
Spring Boot 日常工作中常用Thymeleaf的用法
1:首先,在創(chuàng)建項(xiàng)目的時(shí)候選擇依賴(lài)中選中Thymeleaf,或者在pom中添加依賴(lài)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
或者項(xiàng)目名-右鍵-add Framework Support來(lái)添加依賴(lài)jar包。如圖
2:示例javaBean
此類(lèi)用來(lái)在模板頁(yè)面展示數(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.演示頁(yè)面
根據(jù)默認(rèn)原則,頁(yè)面應(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">訪(fǎng)問(wèn)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)行
訪(fǎng)問(wèn)http://localhost:8080效果如圖:
單擊“獲得名字” f12產(chǎn)看頁(yè)面控制臺(tái)打印的日志效果如圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring+maven實(shí)現(xiàn)郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了spring+maven實(shí)現(xiàn)郵件發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
JAVA中string數(shù)據(jù)類(lèi)型轉(zhuǎn)換詳解
在JAVA中string是final類(lèi),提供字符串不可以修改,string類(lèi)型在項(xiàng)目中經(jīng)常使用,下面給大家介紹了string七種數(shù)據(jù)類(lèi)型轉(zhuǎn)換,需要的朋友可以參考下2015-07-07
關(guān)于replaceFirst使用時(shí)的注意事項(xiàng)
這篇文章主要介紹了關(guān)于replaceFirst使用時(shí)的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
淺談StringBuilder類(lèi)的capacity()方法和length()方法的一些小坑
這篇文章主要介紹了StringBuilder類(lèi)的capacity()方法和length()方法的一些小坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值
今天小編就為大家分享一篇java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
詳解maven的setting配置文件中mirror和repository的區(qū)別
這篇文章主要介紹了詳解maven的setting配置文件中mirror和repository的區(qū)別,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
如何使用SpringBoot進(jìn)行優(yōu)雅的數(shù)據(jù)驗(yàn)證
這篇文章主要介紹了如何使用SpringBoot進(jìn)行優(yōu)雅的數(shù)據(jù)驗(yàn)證,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
通過(guò)Java代碼來(lái)創(chuàng)建view的方法
本文給大家分享通過(guò)java代碼創(chuàng)建view的方法,以TextView為例創(chuàng)建控件的方法,需要的的朋友參考下吧2017-08-08

