我正在尝试获取数据表中组件的客户端ID。问题是jsf自动将行索引放在组件id之前,即
<a id="mappedidentifier_table:1:mappedidentifier_Update" href="#">Update</a>
表示第二行中的链接(index = 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。
有没有人有任何想法?
谢谢,
DZH
答案 0 :(得分:3)
然而,这会返回
mappedidentifier_table:mappedidentifier_Update
而不是mappedidentifier_table:1:mappedidentifier_Update
如果您在行的上下文之外解析clientId
,则会发生这种情况。行上下文由UIData
控件在生命周期的每个阶段设置。
如果您在EL表达式中使用即时评估而不是延迟评估,也可能会发生这种情况 - ${}
而不是#{}
。
顺便说一下,正如文章所述,只有当组件标识符对视图是唯一的时,查找算法才会起作用;规范说它只需要NamingContainer
唯一;如果您在页面上使用唯一的组件ID时要小心,这不一定是个问题。