form1.cn
Make a little progress every day

thymeleaf Exception parsing document: template=

19th of July 2017 Java Java 3314

因为 Thymeleaf 要严格解析XML格式的文档,遇到不闭合标签会报这个错误


比如:

<link th:src="@{bootstrap/css/bootstrap.css}" rel="stylesheet">

就会报 thymeleaf Exception parsing document: template="xxxx"错误


如果:

<link th:src="@{bootstrap/css/bootstrap.css}" rel="stylesheet"/>

单标签的末尾加上结束 / 就不会报这个错误


让 Thymeleaf 不那么严格的解析XML格式的文档方法如下:


1. 添加maven依赖

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>


2. 更改application.properties 或 application.yml的配置属性

spring.thymeleaf.mode=LEGACYHTML5

spring:
    thymeleaf:
        mode: LEGACYHTML5


提示:以上配置后,如果你模板中有未闭合的单标签,系统会自动帮你闭合