SpringBoot中的Thymeleaf模板
一、前言
Thymeleaf 的出現(xiàn)是為了取代 JSP,雖然 JSP 存在了很長時(shí)間,并在 Java Web 開發(fā)中無處不在,但是它也存在一些缺陷:
1、JSP 最明顯的問題在于它看起來像HTML或XML,但它其實(shí)上并不是。大多數(shù)的JSP模板都是采用HTML的形式,但是又摻雜上了各種JSP標(biāo)簽庫的標(biāo)簽,使其變得很混亂。
2、JSP 規(guī)范是與 Servlet 規(guī)范緊密耦合的。這意味著它只能用在基于 Servlet 的Web應(yīng)用之中。JSP模板不能作為通用的模板(如格式化Email),也不能用于非Servlet的 Web 應(yīng)用。
相較于 JSP 來說,Thymeleaf 很好的解決了這些缺點(diǎn):
1、Thymeleaf模板是原生的,不依賴于標(biāo)簽庫。它能在接受原始 HTML 的地方進(jìn)行編輯和渲染。
2、因?yàn)樗鼪]有與Servlet規(guī)范耦合,因此 Thymeleaf 模板能夠進(jìn)入JSP所無法涉足的領(lǐng)域。這意味著Thymeleaf模板與JSP不同,它能夠按照原始的方式進(jìn)行編輯甚至渲染,而不必經(jīng)過任何類型的處理器。當(dāng)然,我們需要Thymeleaf來處理模板并渲染得到最終期望的輸出。即便如此,如果沒有任何特殊的處理,home.html也能夠加載到Web瀏覽器中,并且看上去與完整渲染的效果很類似。
Spring boot不建議使用 JSP 開發(fā)web。
二、集成 Thymeleaf 模板引擎
SpringBoot 對 Thymeleaf 模板引擎的支持也很簡單:
1、pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
這時(shí)候,SpringBoot 對 Thymeleaf 模板的支持就完成了,我們就能在 Web 開發(fā)中使用 Thymeleaf 模板了,簡單吧?
之前的文章有提到 SpringBoot 的關(guān)鍵是 “約定俗成”。既然我們選擇了這么簡單的配置,那么在開發(fā)中就要遵守 SpringBoot 對 Thymeleaf 約定俗成的方案,最重要的一點(diǎn)就是 模板文件放在 templates 目錄下,即模板解析器前綴是 /templates/ ,后綴是 .html 。
2、application.yml
如果不想要所謂約定俗成的方案,想進(jìn)行一些自定義的配置呢?且看下方:
spring: thymeleaf: prefix: classpath:/templates/ suffix: .html servlet: content-type: text/html enabled: true encoding: UTF-8 mode: HTML5 cache: false
3、WebConfig.java
如果上面的配置還不能達(dá)到你的要求,你想要更細(xì)化對 Thymeleaf 的控制,包括配置視圖解析器、模板解析器以及模板引擎這些,那么請看下面的方案!
/**
* 1、ThymeleafViewResolver 接收邏輯視圖名稱將它解析為視圖
* 2、SpringTemplateEngine會(huì)在Spring中啟用Thymeleaf引擎,用來解析模板,并基于這些模板渲染結(jié)果
* 3、TemplateResolver會(huì)最終定位和查找模板。
*/
@Configuration
public class WebConfig {
/**
* 配置 Thymeleaf 視圖解析器 —— 將邏輯視圖名稱解析為 Thymeleaf 模板視圖
*
* @param springTemplateEngine 模板引擎
* @return
*/
@Bean
public ViewResolver viewResolver(SpringTemplateEngine springTemplateEngine){
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(springTemplateEngine);
return resolver;
}
/**
* 模板引擎 —— 處理模板并渲染結(jié)果
*
* @param templateResolver 模板解析器
* @return
*/
@Bean
public SpringTemplateEngine springTemplateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.setTemplateResolver(templateResolver);
return springTemplateEngine;
}
/**
* 模板解析器 —— 加載 Thymeleaf 模板
*
* @return
*/
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
}
三、使用 Thymeleaf 模板
做好了上面的配置后,讓我們來看看如何在 SpringBoot 中使用 Thymeleaf 模板吧:
1、模板文件 — /templates/user/list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h2>用戶列表</h2>
<div>
<ul>
<li th:each="user:${users}">
<span th:text="${user.uuid}"></span>-
<span th:text="${user.name}"></span>-
<span th:text="${user.age}"></span>-
<span th:text="${user.address}"></span>
</li>
</ul>
</div>
</body>
</html>
2、控制層 — ModelAndViews
這里 Model 指的是:控制層處理完請求,返回需要渲染的結(jié)果;Views 指的是:模板的邏輯視圖名(前后端分離)。
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/list")
public String listUser(Model model) {
List<UserDto> userList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
userList.add(new UserDto(UUID.randomUUID().toString().replace("-", ""), "張三" + i, 1, "中國北京"));
}
model.addAttribute("users", userList);
return "user/list";
}
}
3、效果

演示源代碼:https://github.com/JMCuixy/Thymeleaf
總結(jié)
以上所述是小編給大家介紹的SpringBoot中的Thymeleaf模板,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Spring Boot thymeleaf模板引擎的使用詳解
- SpringBoot使用Thymeleaf模板引擎訪問靜態(tài)html的過程
- springBoot加入thymeleaf模板的方式
- spring boot 項(xiàng)目中使用thymeleaf模板的案例分析
- Spring boot項(xiàng)目使用thymeleaf模板過程詳解
- SpringBoot使用thymeleaf模板過程解析
- Spring Boot集成Thymeleaf模板引擎的完整步驟
- Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
- Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
- 詳解spring Boot 集成 Thymeleaf模板引擎實(shí)例
- springboot用thymeleaf模板的paginate分頁完整代碼
- springboot中thymeleaf模板使用詳解
- Springboot Thymeleaf模板文件調(diào)用Java類靜態(tài)方法
- Java基礎(chǔ)總結(jié)之Thymeleaf詳解
相關(guān)文章
Mybatis配置之properties和settings標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之properties和settings標(biāo)簽的用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
教你用springboot連接mysql并實(shí)現(xiàn)增刪改查
今天教各位小伙伴用springboot連接mysql并實(shí)現(xiàn)增刪改查功能,文中有非常詳細(xì)的步驟及代碼示例,對正在學(xué)習(xí)Java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05
SpringBoot EasyPoi動(dòng)態(tài)導(dǎo)入導(dǎo)出的兩種方式實(shí)現(xiàn)方法詳解
項(xiàng)目里使用的是EasyPoi來處理導(dǎo)入導(dǎo)出功能的。近日因業(yè)務(wù)需求調(diào)整,一些導(dǎo)出功能的導(dǎo)出列需要根據(jù)不同的條件動(dòng)態(tài)導(dǎo)出2022-09-09
Java?MethodHandles介紹與反射對比區(qū)別詳解
這篇文章主要為大家介紹了Java?MethodHandles介紹與反射對比區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
利用JWT如何實(shí)現(xiàn)對API的授權(quán)訪問詳解
這篇文章主要給大家介紹了關(guān)于利用JWT如何實(shí)現(xiàn)對API的授權(quán)訪問的相關(guān)資料,需要的朋友可以參考下2018-09-09
BeanUtils.copyProperties使用總結(jié)以及注意事項(xiàng)說明
這篇文章主要介紹了BeanUtils.copyProperties使用總結(jié)以及注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

