Thymeleaf - 严格的HTML解析问题 [英] Thymeleaf - Strict HTML parsing issue

查看:174
本文介绍了Thymeleaf - 严格的HTML解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML5允许一些标签的写法更加宽松,即没有相应的END标签。例如 input 无需关闭< / input> 。但是,如果选择模板模式 HTML5 在Thymeleaf中,Thymeleaf引擎对此抱怨并且不解析HTML模板。我想重写这个默认的Strict标记检查行为。即Thymeleaf应该使用元和输入(AND ALIKE)标签解析HTML模板,而不考虑它们的响应。关闭标签。 PL。

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

< a href =/ home / pic / image.pngdownload =/ path / to / filedata-gallery>< / a>

遇到 data-gallery 时应引发异常然后是'=',这很麻烦,因为它需要HTML5以上的灵活性。

你只需要do是在LEGACYHTML5模式下运行Thymeleaf,它的功能就像一个魅力。感谢这个帖子中,我找到了解决方案,并且正在记录SO,所以其他人不必在寻找这个答案时经历同样的麻烦。



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

 < ;! - 查看TemplateResolver  - > 
< bean id =templateResolverclass =org.thymeleaf.templateresolver.ServletContextTemplateResolver>
< property name =templateModevalue =LEGACYHTML5/>
< property name =cacheablevalue =false/>
< / bean>

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

  spring.thymeleaf.mode = LEGACYHTML5 
spring.thymeleaf.cache = false

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

 <依赖项> 
< groupId> net.sourceforge.nekohtml< / groupId>
< artifactId> nekohtml< / artifactId>
< version> 1.9.21< / version>
< /依赖关系>

Gradle

 'net.sourceforge.nekohtml:nekohtml:1.9.21'


HTML5 allows some tags to be written more liberally i.e without corresponding END tags. e.g. input need not be closed </input>.However if choose template mode HTML5 in Thymeleaf the Thymeleaf engine complains about this and does not parse the HTML template. I want to override this default Strict tag checking behavior. i.e Thymeleaf should parse an HTML template with meta and input (AND ALIKE) tags WITHOUT THEIR RESP. CLOSING TAGS. Pl. guide.

It also complains when you have something like this

<a href="/home/pic/image.png" download="/path/to/file" data-gallery></a>

It throws an exception when it encounters the data-gallery throws "should be followed by '=' " which is kind of annoying as it takes the flexibility out of HTML5.

解决方案

All you have to do is run Thymeleaf in "LEGACYHTML5" mode and it works like a charm. Thanks to this and this post, I found the solution and am documenting in SO so others do not have to go through the same trouble in finding this answer.

To set the legacy mode you can define the bean in your Spring XML file:

<!-- View TemplateResolver -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="templateMode" value="LEGACYHTML5"/>
    <property name="cacheable" value="false"/>
</bean>

or add the properties to the application.properties file:

spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false

And in both cases you have to add the nekohtml jar to your project or, if you are running maven, you can add its dependency to your pom.xml

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

Gradle

'net.sourceforge.nekohtml:nekohtml:1.9.21'

这篇关于Thymeleaf - 严格的HTML解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆