1. thymeleaf簡介
1. Thymeleaf是適用於Web和獨立環境的現代服務器端Java模板引擎。
2. Thymeleaf的主要目標是為您的開發工作流程帶來優雅的自然模板 -HTML可以在瀏覽器中正確顯示,也可以作為靜態原型工作,從而可以在開發團隊中加強協作。
3. Thymeleaf擁有適用於Spring Framework的模塊,與您喜歡的工具的大量集成以及插入您自己的功能的能力,對於現代HTML5 JVM Web開發而言,Thymeleaf是理想的選擇-盡管它還有很多工作要做。
2. thymeleaf特點
1. thymeleaf在有網絡無網絡的環境下都可以運行,所以可以直接在瀏覽器打開查看靜態頁面效果。它支持HTML原型,可以在HTML標簽里面添加其他屬性來實現數據渲染。
2. thymeleaf具有開箱即用的特性,Thymeleaf是Spring boot推薦使用的模版引擎,直接以html顯示,前后端可以很好的分離。
3. thymeleaf在SpringBoot的應用
1. 國際化,渲染不同國家的語言
2. 共同頁面顯示,比如統一異常頁面處理,共同的頁面處理
4. SpringBoot引入Thymeleaf
新建一個Springboot web項目,然后添加以下依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后在配置文件里面添加如下依賴。
spring:
thymeleaf:
cache: false prefix: classpath:/templates/ encoding: UTF-8 #編碼 suffix: .html #模板后綴 mode: HTML #模板
配置說明:
cache這一行是將頁面的緩存關閉,不然我們改變頁面之后可能不能及時看到更改的內容,默認是true。
prefix是配置thymeleaf模板所在的位置。
encoding 是配置thymeleaf文檔的編碼,后面的就不說了
5. controller配置
上面我們配置好了環境之后就可以創建一個controller文件夾,然后寫一個controller,來測試我們的thymeleaf是否成功引入。順便創建一個對象。
代碼:
@Controller public class ThymeleafController { @GetMapping("/getStudents") public ModelAndView getStudent(){ List<Student> students = new LinkedList<>(); Student student = new Student(); student.setId(1); student.setName("全棧學習筆記"); student.setAge(21); Student student1 = new Student(); student1.setId(2); student1.setName("張三"); student1.setAge(22); students.add(student); students.add(student1); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("students",students); modelAndView.setViewName("students"); return modelAndView; } }
代碼解釋 :我們創建一個list,然后在list里面添加數據,一遍一次將數據傳到頁面使用。然后我們創建一個ModelAndView的對象,將list放入這個modeAndView對象中,第一個參數是需要放到model中的屬性名稱相當於是一個鍵,第二個是值,是一個對象。然后利用setViewName方法,設置要跳轉的頁面或者說是將數據傳到對應的頁面。
最外層我們使用了一個 @Controller,這個注解是用來返回一個頁面或者視圖層的。
當然,返回ModelAndView對象只是一種方法,還有其他的方法,比如說下面這樣
@RequestMapping("/getString") public String getString(HttpServletRequest request){ String name = "全棧學習筆記"; request.setAttribute("name",name); return "index.html"; }
利用http的request傳值。
然后還有這樣
@RequestMapping("/getModel") public String getModel(Model model){ model.addAttribute("key","這是一個鍵"); return "index.html"; }
去掉末尾的.html也可以,因為我們在配置文件里面設置了文件的格式為HTML文件。return的字符串都是對應的HTML文件的名稱。
實體類代碼如下:
/** * (Student)實體類 * * @author 全棧學習筆記 * @since 2020-04-14 11:39:10 */ public class Student { private static final long serialVersionUID = -91969758749726312L; /** * 唯一標識id */ private Integer id; /** * 姓名 */ private String name; /** * 年齡 */ private Integer age; //省略get,set方法,自己加上 }
6. 頁面編寫
寫好代碼就等頁面了,在templates文件夾下面創建一個students.html文件,編寫如下代碼
<!DOCTYPE html> <html lang="en" xmlns:th="https://www.thymeleaf.org/"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <tr> <td>ID</td> <td>姓名</td> <td>年齡</td> </tr> <tr th:each="student:${students}"> <td th:text="${student.id}"></td> <td th:text="${student.name}"></td> <td th:text="${student.age}"></td> </tr> </table> </body> </html>
這里有一個很重要的事情就是,我們使用thymeleaf模板之前必須先引入thymeleaf,如下。
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
這個很關鍵,不然你就用不了這個thymeleaf語法規則。
代碼說明:你可以看到th:each 這個語法,是用來遍歷的,類似於for循環,然后我們通過th:text 這個語法來渲染文字。然后還有一些其他的語法,比如說遍歷對象
<div th:object="${student}"> <p th:text="id"></p> <p th:text="name"></p> <p th:text="age"></p> </div>
其他多余的語法規則這里就不一一講解了。
常用的語法:
<!-- 邏輯判斷 --> th:if th:else <!-- 分支控制 --> th:switch th:case <!--循環 --> th:each <!-- 運算 --> <p th:text="${age}%2 == 0"></p> <!-- 賦制value --> th:value <!-- 鏈接 --> th:href
本期講解就到這里,如果你覺得本文對你有用,可以點個贊,點個關注哦!下一期更精彩!wx search 全棧學習筆記!點個關注不迷路。