今天學習了spring boot 整合Thymeleaf模板引擎。發現Thymeleaf功能確實很強大。記錄於此,供自己以後使用。
Thymeleaf:
Thymeleaf是一個java類庫,他是一個xml/xhtml/html5的模板引擎,可以作為mvc的web應用的view層。Thymeleaf還提供了額外的模組與Spring MVC整合,所以我們可以使用Thymeleaf完全替代jsp。spring Boot通過org.springframework.boot.autoconfigure.thymeleaf包對Thymeleaf進行了自動設定。通過ThymeleafAutoConfiguration類對整合所需要的bean進行自動設定。包括templateResolver,templateEngine,thymeleafViewResolver的配置。
下面我將示範spring boot 日常工作中常用的Thymeleaf用法。 Spring Boot 日常工作中常用Thymeleaf的用法
1:首先,在建立項目的時候選擇依賴中選中Thymeleaf,或者在pom中添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
或者項目名-右鍵-add Framework Support來添加依賴jar包。如圖
2:樣本javaBean
此類用來在模板頁面展示資料用。包含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.指令碼樣式靜態檔案
根據預設原則,指令碼樣式,圖片等靜態檔案應放置在src/main/resources/static下,這裡引入了Bootstrap和jQuery,結構如圖所示:
4.示範頁面
根據預設原則,頁面應放置在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="stylesheet"/> <link th:href="@{bootstrap/css/bootstrap-theme.min.css}" 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.資料準備
代碼如下:
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@SpringBootApplicationpublic 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.運行
訪問http://localhost:8080效果如圖:
單擊“獲得名字” f12產看頁面控制台列印的日誌效果如圖: