4

我正在尝试获取数据表中组件的客户端 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。

有人有想法吗 ?

谢谢,

大疆

4

1 回答 1

3

但是这会返回mappedidentifier_table:mappedidentifier_Update而不是mappedidentifier_table:1:mappedidentifier_Update

如果您解析clientId行的外部上下文,则会发生这种情况。行上下文由UIData控件在生命周期的每个阶段设置。

如果您在 EL 表达式中使用立即评估而不是延迟评估 -${}而不是#{}.

顺便说一句,正如文章中所指出的,该查找算法仅在组件标识符对视图是唯一的情况下才有效;规范说它只需要对NamingContainer; 如果您小心在页面上使用唯一的组件 ID,这不是问题。

于 2009-10-01T14:31:05.913 回答