SpringBoot整合thymeleaf 報錯的解決方案
近日 在springboot項目中使用thymeleaf時,莫名報了以下錯誤:

在網(wǎng)上查找文章明白了報錯的原因,這是由于如果使用thymeleaf 為模板,那么解析時就要求html必須為嚴格的html5格式,即必須有完整的結(jié)束標記, 不然就會報錯。
在html頁面中,諸如input,meta,link等標簽 ,是可以不用閉合就可以被解析的(自閉合的),但是由于這里嚴格要求html5格式
于是解決辦法如下:
1) 在報錯的標簽上加入 結(jié)束標簽。
2) 修改為不嚴格的模式。
在配置文件中加入如下配置:
mode: LEGACYHTML5

使用該配置,需要加入以下依賴:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
3) 修改高版本的thymeleaf版本 。
低版本(即spring-boot-starter-parent的版本) SpringBoot 默認使用的thymeleaf 版本為 2.1版本 ,該版本無法識別html5中常見的自閉合標簽。官方原話,可以使用高版本thymeleaf,并通過配置來解決該問題。
可以在pom文件中強行指定thymeleaf的版本 ,如下:

<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
然后再配置文件中配置如下:
thymeleaf:
mode: HTML
事實上,我在使用高版本的springBoot版本(2.0.4.RELEASE)時,并沒有遇到這個情況 ,而該項目中使用的是1.5.9版本的springboot ,這也從側(cè)面證明該方法可行。
Springboot 集成 Thymeleaf 及常見錯誤
Thymeleaf模板引擎是springboot中默認配置,與freemarker相似,可以完全取代jsp,在springboot中,它的默認路徑是src/main/resources/templates 靜態(tài)文件css, js 等文件默認路徑是src/main/resources/static,所有項目中如果沒有這個目錄需要手動加上了

首先我們要在pom.xml文件中添加依賴
<!-- thymeleaf 模板引用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
引用之后我們就來測試一下, 在pom.xml中引入依賴之后。你完全可以不用配置(也秉承了springboot 約定優(yōu)于配置)當然你如果需要自定義一些屬性,你可以在application.properties 中添加配置。
測試類@Controller
/**
* @author pillarzhang
* @date 2019-06-03
*/
@Controller
public class loginController {
@RequestMapping("/index")
public String index(){
return "index";
}
}
Index,html 頁面如下
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> <p style="color:red">hello world</p> </body> </html>
啟動項目,輸入http://localhost:8081/index 即可看到如下頁面

這就成功的集成了Thymeleaf。
注意:前面也說了,如果你不配置任何屬性依然可以使用,當然你也可以自己設(shè)置,在配置文件中application.properties 設(shè)置相應的屬性
spring.thymeleaf.prefix=classpath:/templates/ 設(shè)置thymeleaf路徑默認為src/main/resources/templates spring.thymeleaf.suffix=.html 設(shè)置thymeleaf模板后綴 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false 是否開啟緩存默認為true spring.thymeleaf.mode=LEGACYHTML5 設(shè)置thymeleaf嚴格校驗 spring.thymeleaf.encoding=UTF-8 設(shè)置編碼
1、配置完成之后一定要注意路徑地址是否正確,
2、一定要用@Controller,如果使用@RestController,有可能返回return中的一串字符

3、方法前不要加@ResponseBody,加這個注釋相當于@RestController, 返回一串字符串同上
4、如果載application.properties重配置屬性,一定要注意是否書寫有誤,不能多空格否則有可能會報如下錯誤:

至此,springboot集成thymeleaf 就完成了,雖然中間遇到了一些小問題,還好解決了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Springboot詳解如何整合使用Thymeleaf
- springboot+thymeleaf整合阿里云OOS對象存儲圖片的實現(xiàn)
- springboot整合shiro之thymeleaf使用shiro標簽的方法
- SpringBoot使用thymeleaf實現(xiàn)一個前端表格方法詳解
- Springboot使用thymeleaf動態(tài)模板實現(xiàn)刷新
- 淺析SpringBoot中使用thymeleaf找不到.HTML文件的原因
- springboot如何使用thymeleaf模板訪問html頁面
- springboot中thymeleaf模板使用詳解
- SpringBoot?整合Thymeleaf教程及使用方法
相關(guān)文章
SpringBoot實戰(zhàn)之實現(xiàn)結(jié)果的優(yōu)雅響應案例詳解
這篇文章主要介紹了SpringBoot實戰(zhàn)之實現(xiàn)結(jié)果的優(yōu)雅響應案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09
Java(TM) Platform SE binary 打開jar文件的操作
這篇文章主要介紹了Java(TM) Platform SE binary 打開jar文件的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
MyBatisPlus-QueryWrapper多條件查詢及修改方式
這篇文章主要介紹了MyBatisPlus-QueryWrapper多條件查詢及修改方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringBoot+Email發(fā)送郵件的實現(xiàn)示例
Spring?Boot提供了簡單而強大的郵件發(fā)送功能,本文主要介紹了SpringBoot+Email發(fā)送郵件的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Java+MySQL實現(xiàn)學生信息管理系統(tǒng)源碼
這篇文章主要為大家詳細介紹了Java+MySQL實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11

