spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错

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  # 关闭缓存,开发过程中开启

配置完成之后重启项目即可。

原文地址:https://www.cnblogs.com/mymelody/p/7903906.html