Thymeleaf - 严格的HTML解析问题

Mus*_*eel 22 html html5 thymeleaf

HTML5允许更自由地编写某些标签,即没有相应的END标签.例如,input不需要关闭.</input>但是如果在Thymeleaf中选择模板模式 HTML5,那么Thymeleaf引擎会抱怨这个并且不会解析HTML模板.我想覆盖此默认的严格标记检查行为.即Thymeleaf应解析带有元和输入(AND ALIKE)标签的HTML模板,不带其RESP.关闭标签.PL.指南.

当你有这样的事情时,它也会抱怨

<a href="/home/pic/image.png" download="/path/to/file" data-gallery></a>
Run Code Online (Sandbox Code Playgroud)

当它遇到数据库引发时会引发异常"应该跟着'='"这有点令人讨厌,因为它需要HTML5的灵活性.

Mus*_*eel 58

你所要做的就是以"LEGACYHTML5"模式运行Thymeleaf,它就像一个魅力.多亏了这篇文章这篇文章,我找到了解决方案并且正在进行文档记录,以便其他人不必在找到这个答案时遇到同样的麻烦.

要设置遗留模式,您可以在Spring XML文件中定义bean:

<!-- View TemplateResolver -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="templateMode" value="LEGACYHTML5"/>
    <property name="cacheable" value="false"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

或者将属性添加到application.properties文件中:

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

在这两种情况下,您都必须将nekohtmljar 添加到项目中,或者,如果您正在运行maven,则可以将其依赖项添加到您的pom.xml中

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

摇篮

'net.sourceforge.nekohtml:nekohtml:1.9.21'
Run Code Online (Sandbox Code Playgroud)