我正在尝试在 Wicket 口中创建一个动态表格组件来显示对象列表。该组件将接收对象列表并将其呈现为表格形式。
假设以下产品实体:
public class Product {
public static final long serialVersionUID = 1L;
//
private String id;
private String name;
private String description;
private Double price;
// getters and setters omitted for brevity
}
以下 html/代码对将起作用:
(注意:产品实体稍后将被泛化,因此我们可以向其传递任何 POJO 的列表)
<table>
<tbody>
<tr wicket:id="row">
<td wicket:id="cell">
cell
</td>
</tr>
</tbody>
</table>
----------------------------------------------------------------
parameters:
final String[] fieldNames = new String[]{"id", "name", "description", "price"};
List<Product> productList = ....
----------------------------------------------------------------
ListView lvRows = new ListView("row", productList) {
@Override
protected void populateItem(ListItem item) {
Product product = (Product)item.getModelObject();
CompoundPropertyModel cpm = new CompoundPropertyModel(product);
//
RepeatingView cell = new RepeatingView("cell", cpm);
item.add(cell);
//
for (String fn : fieldNames) {
Label label = new Label(fn);
cell.add(label);
}
}
};
this.add(lvRows);
问题是上面的代码将导致一堆警告(列表中的每个产品一个):
00:57:52.339 [http-apr-8080-exec-108] WARN o.a.w.m.repeater.AbstractRepeater - Child component of repeater org.apache.wicket.markup.repeater.RepeatingView:cell has a non-safe child id of id. Safe child ids must be composed of digits only.
所以我的问题是:
我做错了吗?
如果没有,我该如何摆脱警告?
为什么在此实例中 wicket 需要数字子 ID?通过将 id 链接到对象属性,CompoundPropertyModel 在其他情况下工作正常...
如果我做错了,“正确”的方法是什么?我应该唯一地创建子 id,放弃使用CompoundPropertyModel,并直接通过反射提供值吗?但不确定对性能的影响,为每个单元进行反射调用并不便宜。像这样的事情:
for(String fn:fieldNames} { String s = ...; //find the value of Object O, property fn via Reflection Label label = new Label(cell.newChildId(), s); cell.add(label); }
提前致谢。
最佳答案
您可以通过在 RepeatingView
中添加中间子项来避免警告。 ,正如 Igor Vaynberg 在 this post 中所解释的那样在 Wicket 用户列表中。
换句话说,不要添加 Label
将具有非数字 id 的容器直接添加到中继器,添加一个具有数字 id 的容器,然后添加 Label
到容器:
RepeatingView cell = new RepeatingView("cell");
WebMarkupContainer container = new WebMarkupContainer(cell.newChildId(), cpm);
for (String fn : fieldNames) {
Label label = new Label(fn);
container.add(label);
}
cell.add(container);
item.add(cell);
该帖子中还概述了这些警告的原因。
编辑:
看来您需要建模 ProductPanel
并在RepeatingView
内使用它而不仅仅是 WebMarkupContainer
。这意味着ProductPanel
可能必须在其 HTML 中显式枚举属性(或 wicket:id
s)。
您也可以保留当前代码并执行Label label = new Label(cell.newChildId(), new PropertyModel(product, fn));
如果您希望 HTML 独立于特定属性,请删除 CPM。
关于java - Wicket - 生成动态表 - ChildId 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20879616/