我正在尝试获取数据表中组件的客户端 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。
有人有想法吗 ?
谢谢,
天
最佳答案
however this returns
mappedidentifier_table:mappedidentifier_Update
instead ofmappedidentifier_table:1:mappedidentifier_Update
如果您解决了
clientId
,就会发生这种情况。在一行的上下文之外。行上下文由 UIData
在生命周期的每个阶段设置。控制。如果您在 EL 表达式中使用立即评估而不是延迟评估,也可能会发生这种情况 -
${}
而不是 #{}
.顺便说一句,正如文章中所述,该查找算法仅在组件标识符对于 View 是唯一的情况下才有效;规范说它只需要唯一的
NamingContainer
;如果您小心谨慎地在页面上使用唯一的组件 ID,这应该不是问题。
关于jsf - 如何在数据表中获取组件的jsf clientid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1493217/