使用Spring Boot时,大多数场景下都会使用thymeleaf作为模板文件。默认情况下,thymeleaf要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错。但是大多数情况下我们希望HTML没有严格的标签约束,解决办法如下。
application.yml
spring:
mvc:
view:
prefix: /templates/
suffix: .html
thymeleaf:
cache: false
check-template-location: true
content-type: text/html; charset=utf-8
enabled: true
encoding: UTF-8
prefix: classpath:/templates/
suffix: .html
mode: LEGACYHTML5
mode: LEGACYHTML5,就是声明thymeleaf使用非严格的HTML。但是启动之后访问页面会报如下错误:
org.thymeleaf.exceptions.ConfigurationException: Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in "LEGACYHTML5" mode [http://nekohtml.sourceforge.net].
Maven spec: "net.sourceforge.nekohtml::nekohtml::1.9.15". IMPORTANT: DO NOT use versions of nekoHTML older than 1.9.15.
添加下面的maven依赖即可解决。
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>