springboot 在使用thymeleaf 作為模板時,當出現未關閉標簽時,如下所示代碼,標簽沒有關閉。
<link href="plugin/layui/css/layui.css" rel="stylesheet" >
會出現如下異常:
org.xml.sax.SAXParseException: 元素類型 "meta" 必須由匹配的結束標記 "</meta>" 終止。
可以通過配置thymeleaf屬性並結合nekohtml即可實現關閉對非標准html5標簽的檢查。
首先在項目pom文件中加入對nekohtml的依賴:
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>${nekohtml.version}</version> </dependency>
然后再springboot application配置文件中加入以下配置,properties配置為:spring.thymeleaf.mode=LEGACYHTML5
yml配置文件為:
spring: thymeleaf: mode: LEGACYHTML5 # 不進未關閉標簽檢查,需配合nekohtml使用 cache: false # 關閉緩存,開發過程中開啟
配置完成之后重啟項目即可。