SpringBoot整合Thymeleaf的方法
簡(jiǎn)介:
在目前的企業(yè)級(jí)應(yīng)用開(kāi)發(fā)中 前后端分離是趨勢(shì),但是視圖層技術(shù)還占有一席之地, Spring Boot 對(duì)視圖層技術(shù)提供了很好的支持,官方推薦使用的模板引擎是 Thymeleaf 不過(guò)像 FreeMarker 也支持, JSP 技術(shù)在這里并不推薦使用。
Thymeleaf 是新一代 Java 模板引擎,類(lèi)似于 Velocity、FreeMarker 等傳統(tǒng) Java 模板引擎。與傳統(tǒng) Java 模板引擎不同的是 Thymeleaf 支持 HTML 原型,既可 以讓前端工程師在瀏覽器中直接打 開(kāi)查看樣式,也可以讓后端工程師結(jié)合真實(shí)數(shù)據(jù)查看顯示效果。 同時(shí), Spring Boot 提供了 Thymeleaf 自動(dòng) 配置解決方案,因此Spring Boot 中使用 Thymeleaf 常方便。
1.引入依賴(lài):
<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>
2.application.properties
#是否開(kāi)啟緩存,開(kāi)發(fā)時(shí)可設(shè)置為false,默認(rèn)為true spring.thymeleaf.cache=true #是否檢查模板是否存在,默認(rèn)為true spring.thymeleaf.check-template=true #是否檢查模板位置是否存在,默認(rèn)為true spring.thymeleaf.check-template-location=true #模板文件編碼 spring.thymeleaf.encoding=UTF-8 #模板文件位置 spring.thymeleaf.prefix=classpath:/templates/ #Content-Type配置 spring.thymeleaf.servlet.content-type=text/html #模板文件后綴 spring.thymeleaf.suffix=.html
3.創(chuàng)建實(shí)體類(lèi)和controller類(lèi)
public class Book { private Integer id; private String name; private String author; //省略getter/setter } @Controller public class BookController { @GetMapping("/books") public ModelAndView books() { List<Book> books = new ArrayList<>(); Book b1 = new Book(); b1.setId(1); b1.setAuthor("羅貫中"); b1.setName("三國(guó)演義"); Book b2 = new Book(); b2.setId(2); b2.setAuthor("曹雪芹"); b2.setName("紅樓夢(mèng)"); books.add(b1); books.add(b2); ModelAndView mv = new ModelAndView(); mv.addObject("books", books); mv.setViewName("books"); return mv; } }
4.html文件:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>圖書(shū)列表</title> </head> <body> <table border="1"> <tr> <td>圖書(shū)編號(hào)</td> <td>圖書(shū)名稱(chēng)</td> <td>圖書(shū)作者</td> </tr> <tr th:each="book:${books}"> <td th:text="${book.id}"></td> <td th:text="${book.name}"></td> <td th:text="${book.author}"></td> </tr> </table> </body> </html>
5.結(jié)果:
總結(jié)
以上所述是小編給大家介紹的SpringBoot整合Thymeleaf的方法,希望對(duì)大家有所幫助!
- 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的方法
- SpringBoot Security安裝配置及Thymeleaf整合
- springboot2.1.7整合thymeleaf代碼實(shí)例
- Springboot詳解如何整合使用Thymeleaf
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(58)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-08-08使用Spring MVC實(shí)現(xiàn)雙向數(shù)據(jù)綁定
Spring MVC是一個(gè)廣泛用于構(gòu)建Java Web應(yīng)用程序的框架,它提供了眾多功能,包括雙向數(shù)據(jù)綁定,在這篇文章中,我們將向Java新手介紹如何使用Spring MVC實(shí)現(xiàn)雙向數(shù)據(jù)綁定,以及為什么這個(gè)特性如此重要,需要的朋友可以參考下2024-01-01Spring Boot(四)之使用JWT和Spring Security保護(hù)REST API
這篇文章主要介紹了Spring Boot(四)之使用JWT和Spring Security保護(hù)REST API的相關(guān)知識(shí),需要的朋友可以參考下2017-04-04深入分析:用1K內(nèi)存實(shí)現(xiàn)高效I/O的RandomAccessFile類(lèi)的詳解
本篇文章是對(duì)用1K內(nèi)存實(shí)現(xiàn)高效I/O的RandomAccessFile類(lèi)的詳細(xì)分析介紹,需要的朋友參考下2013-05-05springboot:接收date類(lèi)型的參數(shù)方式
這篇文章主要介紹了springboot:接收date類(lèi)型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10使用Spring?Cloud?Stream處理事件的示例詳解
Spring?Cloud?Stream?是基于?Spring?Boot?的用于構(gòu)建消息驅(qū)動(dòng)微服務(wù)的框架,本文主要介紹了如何使用?Spring?Cloud?Stream?來(lái)處理事件,需要的可以參考一下2023-06-06Spring Data Jpa實(shí)現(xiàn)分頁(yè)和排序代碼實(shí)例
本篇文章主要介紹了Spring Data Jpa實(shí)現(xiàn)分頁(yè)和排序代碼實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03Spring中bean對(duì)象的裝配方式、作用域及生命周期詳解
這篇文章主要介紹了Spring中bean對(duì)象的裝配方式、作用域及生命周期詳解,SprignBoot中?@Bean?完美的替換了了上面的這種在xml中配置的方法,使用以下方法就能讓spring在需要自動(dòng)創(chuàng)建Info對(duì)象時(shí),自動(dòng)調(diào)用這個(gè)方法,需要的朋友可以參考下2023-11-11Eclipse Web項(xiàng)目打成war包的方法圖解
當(dāng)Tomcat啟動(dòng)后該壓縮文件自動(dòng)解壓縮,war包方便了web工程的發(fā)布,那么Eclipse中如何將Web項(xiàng)目打成war包呢?下面小編通過(guò)圖文并茂的方式給大家講解下Eclipse Web項(xiàng)目打成war包的方法,一起看看吧2016-08-08