使用 Wicket 向 html 元素添加属性

Adding an attribute to html element using Wicket

我需要将属性 display-name 添加到 <td>,并将值作为 wicket id。

<tbody>
  <tr wicket:id="result" class="bgalternate1">
    <td wicket:id="values">
    <span wicket:id="value">Value</span>
    </td>
  </tr>
</tbody>

预期结果是

<tbody>
  <tr class="bgalternate1">
    <td display-name="<attribute_name>"> <!--attribute is column header -->
    <span wicket:id="value">Value</span>
    </td>
  </tr>
</tbody>

我尝试使用下面的 java 代码,但是无法达到预期的结果,即无法将属性 "display-name" 附加到 <td>。我最初使用的是 SimpleAttributeModifier,因为它已被弃用,使用了 AttributeModifier。

public ReportResultPanel(final String id, final IModel<AdHocReportSetup> model)
{
super(id, model);
setOutputMarkupId(true);

final IModel<Paging> pagingModel = new Model<Paging>(new Paging());
resultModel = createResultModel(model, pagingModel);
addResults(this, "result", resultModel);
}

private ListView<AdHocReportResult> addResults(final MarkupContainer parent, 
final String id,
        final IModel<ReportResultModel> model)
{
    final IModel<List<AdHocReportResult>> resultsModel = new PropertyModel<List<AdHocReportResult>>(model, "results");
    final ListView<AdHocReportResult> result = new ListView<AdHocReportResult>(id, resultsModel) {

    private static final long serialVersionUID = 1L;

    @Override
        protected void populateItem(final ListItem<AdHocReportResult> item)
        {
            addResultValues(item, "values", item.getModel());
            AdHocPage.applyParityClass(item.getIndex(), item);

            final Behavior behavior = AttributeModifier.append("display-name", "red");
            for (final Component next : item)
            {
                next.add(behavior);
            }
        }
    };
    parent.add(result);
    return result;

}

private ListView<AdHocReportResultAttribute> addResultValues(final 
MarkupContainer parent, final String id,
        final IModel<AdHocReportResult> model)
{
final IModel<List<AdHocReportResultAttribute>> attributesModel = new PropertyModel<List<AdHocReportResultAttribute>>(
            model, "attributes");

    final ListView<AdHocReportResultAttribute> result = new ListView<AdHocReportResultAttribute>(id, attributesModel) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(final ListItem<AdHocReportResultAttribute> item)
        {
            addValueLabel(item, "value", item.getModel());
        }
    };
    parent.add(result);
    return result;

}

您正在将 AttributeModifier 添加到 ListView,但您确实需要将其添加到它的 ListItem

...
item.add(AttributeModifier.append("display-name", "red"));
addValueLabel(item, "value", item.getModel());
...