我正在尝试获取datatable中组件的客户端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
发布于 2009-10-01 14:31:06
但是,这将返回
mappedidentifier_table:mappedidentifier_Update而不是mappedidentifier_table:1:mappedidentifier_Update
如果您在行的上下文之外解析clientId,就会发生这种情况。行上下文由UIData控件在生命周期的每个阶段设置。
如果在EL表达式中使用立即求值而不是延迟求值- ${}而不是#{},也可能会发生这种情况。
顺便说一句,正如本文中所指出的,这种查找算法只有在组件标识符对于视图是唯一的时才会起作用;规范说它只需要对于NamingContainer__是唯一的;如果您在页面上使用唯一的组件it时小心一点,这不是问题。
https://stackoverflow.com/questions/1493217
复制相似问题