Spring?Boot?整合?Thymeleaf?實(shí)例分享
一、什么是 Thymeleaf
Thymeleaf 是新一代的 Java 模板引擎,類似于 Velocity、FreeMarker 等傳統(tǒng)引擎,其語(yǔ)言和 HTML 很接近,而且擴(kuò)展性更高;
Thymeleaf 的主要目的是將優(yōu)雅的模板引入開(kāi)發(fā)工作流程中,并將 HTML 在瀏覽器中正確顯示。同時(shí)能夠作為靜態(tài)引擎,讓開(kāi)發(fā)成員之間更方便協(xié)作開(kāi)發(fā);
Spring Boot 官方推薦使用模板,而且 Spring Boot 也為 Thymeleaf 提供了完整的自動(dòng)化 配置解決方案;
Thymeleaf 使用教程請(qǐng)戳 Tutorial: Using Thymeleaf,配合 Spring 使用的教程請(qǐng)戳 Tutorial: Thymeleaf + Spring。
二、整合過(guò)程
準(zhǔn)備過(guò)程
正式開(kāi)始整合過(guò)程之前,這里先給出本文的搭建環(huán)境,方便大家進(jìn)行后續(xù)內(nèi)容的學(xué)習(xí)。
- JDK 11(理論上其他版本的 JDK 也是可以的,但是更為推薦 JDK 1.8 及以后的版本)
- IDEA(這里沒(méi)有啥要求,但我個(gè)人的話是出新的版本我就會(huì)更新,雖然臃腫,但是更新了確實(shí)好用 ??)
- SpringBoot 2.x(現(xiàn)在主流應(yīng)該都是 2.x 版本,1.x 的都是老一點(diǎn)的版本了)
添加 Thymeleaf 依賴
添加 Thymeleaf 依賴有兩種方式:
- 第一種
在新建項(xiàng)目時(shí)添加,在 Templeate Engines 中勾選 Thymeleaf;
- 第二種
對(duì)于忘記在新建項(xiàng)目時(shí)未添加 Thymeleaf 依賴的項(xiàng)目,可以直接在項(xiàng)目的 pom.xml 中手動(dòng)添加依賴即可;
<dependency> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
編寫(xiě)實(shí)體類和 Controller
- 新建實(shí)體類 User
這里因?yàn)槭褂?Lombok,所以省去了各種 setter、getter,同時(shí)還省去了各種構(gòu)造方法和重寫(xiě) toString() 等方法,大大簡(jiǎn)化了代碼。而我們所要做的,僅僅是在 pom.xml 中添加 Lombok 的依賴,然后在我們的實(shí)體類中加入對(duì)應(yīng)的注解即可。
以下是在 pom.xml 中插入 Lombok 依賴的對(duì)應(yīng)代碼。
<dependency> ? ? <groupId>org.projectlombok</groupId> ? ? <artifactId>lombok</artifactId> ? ? <optional>true</optional> </dependency>
然后我們就可以編寫(xiě)我們的實(shí)體類,這里主要用到了 @Data、@Component、@AllArgsConstructor 、NoArgsConstructor 四個(gè)注解,其中各個(gè)注解的含義如下:
- @Component:把類實(shí)例化到 Spring 容器,相當(dāng)于在配置文件中配置;
- @Data :給類的所有屬性提供 get 和 set 方法,此外還有 equals、canEqual、hashCode、toString 方法以及 默認(rèn)參數(shù)為空的構(gòu)造方法;
- @AllArgsConstructor:為類提供一個(gè) 全參構(gòu)造方法,但此時(shí)不再提供默認(rèn)構(gòu)造方法;
- @NoArgsConstructor:因?yàn)槭褂昧?AllArgsConstructor 會(huì)導(dǎo)致類沒(méi)有默認(rèn)空參構(gòu)造方法,所以此時(shí)需要它為類提供一個(gè) 無(wú)參構(gòu)造方法;
package com.cunyu.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; /** ?* @author : cunyu ?* @version : 1.0 ?* @className : User ?* @description : User 實(shí)體類 ?*/ @Component @Data @AllArgsConstructor @NoArgsConstructor public class User { ? ? private int age; ? ? private String name; ? ? private String email; }
- 編寫(xiě) Controller
此時(shí)主要需要注意的是 setViewName() 和 addObject(),前者表示方法對(duì)應(yīng)的前端頁(yè)面,也就是我們模板中對(duì)應(yīng)文件名的 .html 文件,而后者則主要給屬性注入值,然后將屬性傳遞到前端模板。
package com.cunyu.controller; import com.cunyu.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** ?* @author : cunyu ?* @version : 1.0 ?* @className : UserController ?* @date : 2020/7/29 16:22 ?* @description : UserController ?*/ @Controller public class UserController { ? ? // 訪問(wèn) ip:port/index ? ? @GetMapping("/index") ? ? public ModelAndView index() { ? ? ? ? ModelAndView modelAndView = new ModelAndView(); ? ? ? ? // 設(shè)置跳轉(zhuǎn)的視圖,即位于 templates/index.html ? ? ? ? modelAndView.setViewName("index"); ? ? ? ? modelAndView.addObject("title", "Thymeleaf 使用"); ? ? ? ? modelAndView.addObject("desc", "Spring Boot 整合 Thymeleaf"); ? ? ? ? User author = new User(25); ? ? ? ? modelAndView.addObject("author", author); ? ? ? ? return modelAndView; ? ? } }
創(chuàng)建Thymeleaf 模板
第上面的代碼中,我們?cè)O(shè)置了跳轉(zhuǎn)的視圖為 index,所以我們需要在 src/main/resources/templates 中創(chuàng)建 index.html。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ? ? ? xmlns:th="http://www.thymeleaf.org"> <head> ? ? <meta charset="UTF-8"> ? ? <!-- 即 Controller 中的 title 屬性 --> ? ? <title th:text="${title}"></title> </head> <body> <!-- 即 Controller 中的 desc 屬性 --> <h1 th:text="${desc}" th:align="center"></h1> <!-- 即 Controller 中的 author 信息 --> <h2 th:align="center">=====作者信息=====</h2> <p th:text="${author?.name}"></p> <p th:text="${author?.age}"></p> <p th:text="${author?.email}"></p> </body> </html>
三、測(cè)試
啟動(dòng)項(xiàng)目,然后在瀏覽器中訪問(wèn) http://localhost:8080/index
,如果出現(xiàn)下圖中的信息,說(shuō)明整合成功。
注意事項(xiàng):
為了方便使用,我們?cè)谑褂?Thymeleaf 模板時(shí),可以添加一些自己的配置。而添加的位置則是項(xiàng)目的配置文件 application.yml,項(xiàng)目默認(rèn)配置文件應(yīng)該是 application.properties,但 SpringBoot 更加推薦使用 yml 來(lái)配置,所以我們這里需要手動(dòng)將其改為 yml 的格式。
spring: ? thymeleaf: ? ? cache: false ? ? prefix: classpath:/templates/ ? ? suffix: .html ? ? mode: HTML ? ? encoding: UTF-8 ? ? servlet: ? ? ? content-type: text/html
總結(jié):
本文主要介紹了 Themeleaf 的相關(guān)簡(jiǎn)介,然后對(duì)利用 SpringBoot 整合 Thymeleaf 的過(guò)程進(jìn)行了描述,最后則是使用 Thymeleaf 中常用的一些相關(guān)配置的注意事項(xiàng)。
到此這篇關(guān)于Spring Boot 整合 Thymeleaf 詳情的文章就介紹到這了,更多相關(guān)Spring Boot 整合 Thymeleaf 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot+thymeleaf整合阿里云OOS對(duì)象存儲(chǔ)圖片的實(shí)現(xiàn)
- SpringBoot整合Thymeleaf小項(xiàng)目及詳細(xì)流程
- springboot整合shiro之thymeleaf使用shiro標(biāo)簽的方法
- SpringBoot整合thymeleaf 報(bào)錯(cuò)的解決方案
- SpringBoot整合Thymeleaf的方法
- Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)
- SpringBoot Security安裝配置及Thymeleaf整合
- SpringBoot整合Thymeleaf的方法
- springboot2.1.7整合thymeleaf代碼實(shí)例
- Spring Boot 整合 Shiro+Thymeleaf過(guò)程解析
相關(guān)文章
spring-data-elasticsearch @Field注解無(wú)效的完美解決方案
這篇文章主要介紹了spring-data-elasticsearch @Field注解無(wú)效的完美解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實(shí)例
這篇文章主要介紹了Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02解決springboot+shiro 權(quán)限攔截失效的問(wèn)題
這篇文章主要介紹了解決springboot+shiro 權(quán)限攔截失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09springboot文件上傳時(shí)maxPostSize設(shè)置大小失效問(wèn)題及解決
這篇文章主要介紹了springboot文件上傳時(shí)maxPostSize設(shè)置大小失效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07JavaWeb項(xiàng)目中classpath路徑詳解
今天小編就為大家分享一篇關(guān)于JavaWeb項(xiàng)目中classpath路徑詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Java8 Optional判空詳解(簡(jiǎn)化判空操作)
這篇文章主要給大家介紹了關(guān)于Java8 Optional判空(簡(jiǎn)化判空操作)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Java整合Jackson實(shí)現(xiàn)反序列化器流程
Jackson是一個(gè)開(kāi)源的Java序列化和反序列化工具,可以將Java對(duì)象序列化為XML或JSON格式的字符串,以及將XML或JSON格式的字符串反序列化為Java對(duì)象。由于其使用簡(jiǎn)單,速度較快,且不依靠除JDK外的其他庫(kù),被眾多用戶所使用2023-01-01深入學(xué)習(xí)springboot線程池的使用和擴(kuò)展
這篇文章主要介紹了深入學(xué)習(xí)springboot線程池的使用和擴(kuò)展,springboot框架提供了@Async注解,幫助我們更方便的將業(yè)務(wù)邏輯提交到線程池中異步執(zhí)行,需要的朋友可以參考下2019-06-06Spring?Data?JPA映射自定義實(shí)體類操作
這篇文章主要介紹了Spring?Data?JPA映射自定義實(shí)體類操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11