springboot使用Thymeleaf報錯常見的幾種解決方案
一、問題
我們使用springboot和Thymeleaf的時候可能會報下面的錯誤:

Exception processing template “index”: An error happened during template parsing (template: “class path resource [templates/index.html]”)
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: “class path resource [templates/index.html]”)
二、解決方法
在springboot后端代碼沒有寫錯的前提下:
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
}1.application.properties配置
注意:一般情況下,問題不會來自于application.properties配置,默認不用配置即可,則選用模板路徑為:spring.thymeleaf.prefix=classpath:/templates/
但是如果配置了該選項,修改了默認路徑,記得創(chuàng)建對應路徑。
2.模板文件頭
保證html文件頭為:
<!DOCTYPE html> <html lang="en">
或者選用html約束
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org">
3.模板文件內(nèi)容
我們在導入寫好的html文件之后一般有幾個問題:
1.html文件頭約束,也就是上面的問題
2.html標簽閉合
3.Thymeleaf的引用需要修改,如一些form表單
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Cloud Gateway打造可擴展的微服務網(wǎng)關
微服務網(wǎng)關是一個位于客戶端和后端微服務之間的服務器,用于處理所有與客戶端的通信,Spring Cloud Gateway都是一個值得考慮的選擇,它將幫助您更好地管理和保護您的微服務,感興趣的朋友一起看看吧2023-11-11
MyBatis-Plus介紹及Spring Boot 3集成指南
本文介紹了MyBatis-Plus的基本特性及其與Spring Boot 3的集成步驟,通過使用MyBatis-Plus,開發(fā)者可以快速地搭建和開發(fā)數(shù)據(jù)訪問層,同時提高代碼質(zhì)量和開發(fā)效率,感興趣的朋友一起看看吧2024-05-05
Java 遍歷取出Map集合key-value數(shù)據(jù)的4種方法
這篇文章主要介紹了Java 遍歷取出Map集合key-value數(shù)據(jù)的4種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09

