SpringBoot项目中使用Thymeleaf时使用非严格的HTML

在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错!解决办法如下:

须有结束标签,否则会报错!解决办法如下:

1、你可以使用严格的标签,也就是每个标签都有结束标签。 比如你在使用Vue.js这样的库,然后有

<div v-cloak></div>
    1

这样的html代码,也会被thymeleaf认为不符合要求而抛出错误。

这种方式可能会比较麻烦,每次都需要手动添加。

2、在application.properties中增加

spring.thymeleaf.content-type=text/html 
spring.thymeleaf.cache=false 
spring.thymeleaf.mode =LEGACYHTML5
    1 2 3

即声明thymeleaf使用非严 格的html。启动之后访问页面会报如下错误:

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.
    1

翻译过来是:

无法执行从遗留html到XML的转换:nekohtml库不处于classpath.nekohtml 1.9.15或更高版本中,需要在“legacyhtml 5”模式下处理模板[http://nekohtml.Source健忘.net]。maven spec:“net.SourceMem.nekohtml::nekohtml::1.9.15”。重要的是:不要使用早于1.9.15的nekohtml版本。

解决方法:

修改pom.xml 添加以下依赖

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

经验分享 程序员 微信小程序 职场和发展