在Thymeleaf中创建空的数据属性 [英] Creating empty data-attributes in Thymeleaf

查看:328
本文介绍了在Thymeleaf中创建空的数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Thymeleaf 3.0.3(春季4.3.7)中创建一个具有自定义数据属性的元素:

I'm attempting to create an element in Thymeleaf 3.0.3 (Spring 4.3.7) with a custom data-attribute:

<input th:attr="data-customAttr=${item.getMyAttr()}" />

如果item.getMyAttr()的结果是'someVal',则呈​​现的HTML会输出到是:

If the result of item.getMyAttr() was 'someVal', then the rendered HTML comes out to be:

<input data-customAttr='someVal' />

但是,如果item.getMyAttr()的结果为空字符串,则会抛出自定义属性完全由Thymeleaf淘汰。呈现的HTML看起来像:

However, if the result of item.getMyAttr() is an empty string, the custom attribute is thrown out by Thymeleaf altogether. The rendered HTML looks like:

<input />

我需要知道自定义属性是否已定义,为空,缺失或未定义。根据此讨论:空的HTML5数据属性是否有效?
,空的数据属性应该是完全有效的。

I need to know whether the custom attribute was defined and empty or missing and undefined. According to this discussion: Are empty HTML5 data attributes valid? , empty data-attributes should be perfectly valid.

经过一番挖掘之后,我遇到了以下测试用例,似乎表明Thymeleaf可以不会渲染空的数据属性,这就是为什么它在渲染之前将其丢弃。以下Thymeleaf代码段呈现得很好:

After a little digging I ran into the following test case that seems to show Thymeleaf can't render empty data-attributes, which is why it's throwing them out before render-time. The following Thymeleaf snippet renders just fine:

<input th:attr="__${'data-customAttr=' + 'someVal'}__" />

虽然两者都引发错误:

<!-- Throws: 'org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence' -->
<input th:attr="__${'data-customAttr=' + ''}__" />
<input th:attr="__${'data-customAttr'}__" />

这是Thymeleaf中的错误吗?任何帮助将不胜感激。我也很高兴提供其他任何相关信息。

Is this a bug in Thymeleaf? Any help would be greatly appreciated. I'm happy to provide any other relevant information as well.

编辑:这是我在相当大的应用程序中经常使用的一项功能。虽然我知道有几种解决方法,但它们都使我完成一个非常简单的任务所需编写/维护的代码量增加了。我希望有一个长期解决方案,即使该解决方案是这是一个错误,请报告。

This is a feature I'm making heavy use of in a fairly large application. While I'm aware that there are several workarounds, they all compound the amount of code I have to write/maintain just to accomplish a very simple task. I'm hoping for a long-term solution, even if that solution is "This is a bug, please report it".

推荐答案

__ 语法没有什么特别的……只是使它两次运行评估程序。之所以不起作用,是因为在每种情况下,它都会解析为无效的百里香叶表达。

The __ syntax isn't anything special... it just makes it run the evaluator twice. The reason it doesn't work is because in each case, it resolves to an invalid thymeleaf expression.

例如:

<input th:attr="__${'data-customAttr=' + ''}__" />
<!-- evaluates to -->
<input th:attr="data-customAttr=" />
<!-- which is an invalid expression -->

<input th:attr="__${'data-customAttr'}__" />
<!-- evaluates to -->
<input th:attr="data-customAttr" />
<!-- which is again, an invalid expression -->

百里香叶确实不会添加空属性。因此,我认为您要做的最好的事情是这样的:

It does appear that thymeleaf won't add empty attributes. So I think the best you are going to do is something like this:

<input th:attr="data-customAttr-exists=${item.getMyAttr() != null}, data-customAttr=${item.getMyAttr()}" />

然后您必须检查第一个属性以了解其是否存在,第二个值的属性。这应该可以让您了解所有情况。

And you'll have to check the first property to know whether or not it exists, and the second property for the value. This should allow you to know all cases.

这篇关于在Thymeleaf中创建空的数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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