在 Thymeleaf 中创建空数据属性
Creating empty data-attributes in Thymeleaf
我正在尝试使用自定义数据属性在 Thymeleaf 3.0.3 (Spring 4.3.7) 中创建一个元素:
<input th:attr="data-customAttr=${item.getMyAttr()}" />
如果 item.getMyAttr() 的结果是 'someVal',那么呈现的 HTML 结果是:
<input data-customAttr='someVal' />
但是,如果 item.getMyAttr() 的结果是一个空字符串,自定义属性将被 Thymeleaf 完全丢弃。呈现的 HTML 看起来像:
<input />
我需要知道自定义属性是已定义但为空还是缺失且未定义。根据这个讨论:Are empty HTML5 data attributes valid?
,空数据属性应该是完全有效的。
经过一些挖掘后,我 运行 进入了以下测试用例,它似乎表明 Thymeleaf 无法呈现空数据属性,这就是它在呈现时间之前将它们丢弃的原因。以下 Thymeleaf 片段呈现得很好:
<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 中的错误吗?任何帮助将不胜感激。我也很乐意提供任何其他相关信息。
编辑:这是我在一个相当大的应用程序中大量使用的功能。虽然我知道有几种解决方法,但它们都增加了我必须 write/maintain 完成一个非常简单的任务的代码量。我希望有一个长期的解决方案,即使该解决方案是 "This is a bug, please report it".
__
语法没有什么特别的……它只是使 运行 两次计算器。它不起作用的原因是因为在每种情况下,它都会解析为无效的 thymeleaf 表达式。
例如:
<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 -->
thymeleaf 似乎不会添加空属性。所以我认为你要做的最好的事情是这样的:
<input th:attr="data-customAttr-exists=${item.getMyAttr() != null}, data-customAttr=${item.getMyAttr()}" />
并且您必须检查第一个 属性 以了解它是否存在,然后检查第二个 属性 以了解其值。这应该让你知道所有的情况。
我正在尝试使用自定义数据属性在 Thymeleaf 3.0.3 (Spring 4.3.7) 中创建一个元素:
<input th:attr="data-customAttr=${item.getMyAttr()}" />
如果 item.getMyAttr() 的结果是 'someVal',那么呈现的 HTML 结果是:
<input data-customAttr='someVal' />
但是,如果 item.getMyAttr() 的结果是一个空字符串,自定义属性将被 Thymeleaf 完全丢弃。呈现的 HTML 看起来像:
<input />
我需要知道自定义属性是已定义但为空还是缺失且未定义。根据这个讨论:Are empty HTML5 data attributes valid? ,空数据属性应该是完全有效的。
经过一些挖掘后,我 运行 进入了以下测试用例,它似乎表明 Thymeleaf 无法呈现空数据属性,这就是它在呈现时间之前将它们丢弃的原因。以下 Thymeleaf 片段呈现得很好:
<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 中的错误吗?任何帮助将不胜感激。我也很乐意提供任何其他相关信息。
编辑:这是我在一个相当大的应用程序中大量使用的功能。虽然我知道有几种解决方法,但它们都增加了我必须 write/maintain 完成一个非常简单的任务的代码量。我希望有一个长期的解决方案,即使该解决方案是 "This is a bug, please report it".
__
语法没有什么特别的……它只是使 运行 两次计算器。它不起作用的原因是因为在每种情况下,它都会解析为无效的 thymeleaf 表达式。
例如:
<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 -->
thymeleaf 似乎不会添加空属性。所以我认为你要做的最好的事情是这样的:
<input th:attr="data-customAttr-exists=${item.getMyAttr() != null}, data-customAttr=${item.getMyAttr()}" />
并且您必须检查第一个 属性 以了解它是否存在,然后检查第二个 属性 以了解其值。这应该让你知道所有的情况。