Spring Boot和Thymeleaf - 删除严格的HTML错误检查

Ozi*_*ile 9 html java spring spring-mvc thymeleaf

我使用Spring Boot作为MVC应用程序,我的视图技术是Thymeleaf.我需要做的一件事就是复制现有网站的HTML(而不是我做的......)并使用Thymeleaf渲染它.然而,一些网站的HTML源代码的包含未闭合的HTML标签(例如<meta>,<link>,<input>),或者未用引号括起来的元素,例如HTML标签:

<div id=1></div>
Run Code Online (Sandbox Code Playgroud)

代替

<div id="1"></div>
Run Code Online (Sandbox Code Playgroud)

当然在浏览器中这可以工作......但是Thymeleaf不允许这样做,也不会为页面提供服务.有没有办法让这个更宽松的规则?我搜索了Thymeleaf的文档和Spring Boot参考,但没有找到答案.

只是为了澄清 - 我甚至没有为Thyemeleaf配置我自己的bean,只是通过maven作为spring-boot-starters之一将它添加到类路径中.所以现在这些都是默认设置.

小智 13

我知道我会在很长一段时间后给出答案,但如果它对任何人都有帮助,那么分享它是件好事.
在将一个属性
"spring,thymeleaf.mode"设置为"LEGACYHTML5" 后,我解决了这个问题.

 spring.thymeleaf.mode=LEGACYHTML5
Run Code Online (Sandbox Code Playgroud)

在pom.xml中,添加依赖项:

<dependency>
  <groupId>net.sourceforge.nekohtml</groupId>
  <artifactId>nekohtml</artifactId>
  <version>1.9.21</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如果你想禁用百里香的缓存,那么百万美元的缓存

spring.thymeleaf.cache=false
Run Code Online (Sandbox Code Playgroud)


don*_*ono 5

Spring Boot 1.5.3支持Thymeleaf 3。Thymeleaf 3 具有完整的 html5 标记支持。

pom.xml在 Spring boot 中添加以下行以覆盖 Thymeleaf 版本,您将能够使用未关闭的标签。

<properties>
   <thymeleaf.version>3.0.6.RELEASE</thymeleaf.version>
   <thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
    ...
</properties>
Run Code Online (Sandbox Code Playgroud)


xdh*_*ore 4

正如 @mussdroid 所说,一切都需要采用有效的 XML 格式。以下是 Thymeleaf 文档的一部分,解释了这一点的背景:http://www.thymeleaf.org/doc/articles/fromhtmltohtmlviahtml.html

另外,如果这是一个问题,我相信您可以打开旧模式以允许非 XML 模板,但如果可能的话我更喜欢使用有效的 XML: http ://www.thymeleaf.org/doc/tutorials/2.1/ usingthymeleaf.html#what-kind-of-templates-can-thymeleaf-process

我自己不知道如何更改模式,但我确信 DuckDuckGo 或这个网站上的某人知道。