项目列表中的Spring Form Taglib和自定义属性

Spring Form Taglib and Custom Attributes from Item List

本文关键字:Taglib 自定义属性 Form Spring 列表 项目      更新时间:2023-09-26

我有一个项目列表,比如types。我可以显示<select>标签,如下所示:

<form:select path="typeId" items="${types}" itemLabel="name" itemValue="typeId"/>

我想将Type对象的description属性添加到<option>标记上的data-属性中,以便与javascript一起使用。

这不起作用,但只是为了提供一个例子:

<form:select path="typeId"/>
    <form:options items="${types}" itemLabel="name" itemValue="typeId" data-description="description"/>
</form:select>

如何访问description属性以填充data-description属性?

您需要实现自己的标记(可能基于表单:options),但据我所知,还没有扩展标记的概念。(抱歉)

根据TLD,

数据描述不是有效的属性,因此任何自定义属性都可能引发异常。

您可以:

  1. 使用c:forEach OR使用标准标记库构建select和options
  2. 将现有未使用的属性重新用于JavaScript。可能是"title"属性