我正在尝试获取数据表中组件的客户端 ID。问题是jsf自动将行索引放在组件ID之前,即
<a id="mappedidentifier_table:1:mappedidentifier_Update" href="#">Update</a>
对于第二行中的链接(索引 = 1)。
我正在使用以下方法来获取clientId
public static String findClientId(String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot view = context.getViewRoot();
if (view == null) {
throw new IllegalStateException("view == null");
}
UIComponent component = findComponent(view, id);
if (component == null) {
throw new IllegalStateException("no component has id='" + id + "'");
}
return component.getClientId(context);
}
private static UIComponent findComponent(UIComponent component, String id) {
if (id.equals(component.getId())) {
return component;
}
Iterator<UIComponent> kids = component.getFacetsAndChildren();
while (kids.hasNext()) {
UIComponent kid = kids.next();
UIComponent found = findComponent(kid, id);
if (found != null) {
return found;
}
}
return null;
}
但是这会返回
mappedidentifier_table:mappedidentifier_Update
,
代替
mappedidentifier_table:1:mappedidentifier_Update
,
因此它不匹配任何元素,导致 id 中的行索引丢失。
我读过http://illegalargumentexception.blogspot.com/2009/05/jsf-using-component-ids-in-data-table.html
但是我打算有一个更简单的实现,而不是像作者那样的 TLD 函数或 facelets。
有人有想法吗 ?
谢谢,
大疆