首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在datatable中获取组件的jsf clientid?

如何在datatable中获取组件的jsf clientid?
EN

Stack Overflow用户
提问于 2009-09-29 15:14:53
回答 1查看 6.7K关注 0票数 4

我正在尝试获取datatable中组件的客户端id。问题是jsf自动将行索引放在组件id之前,即

代码语言:javascript
复制
   <a id="mappedidentifier_table:1:mappedidentifier_Update" href="#">Update</a>

用于第二行中的链接(index=1)。

我使用以下方法来获取clientId

代码语言:javascript
复制
   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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-10-01 14:31:06

但是,这将返回mappedidentifier_table:mappedidentifier_Update而不是mappedidentifier_table:1:mappedidentifier_Update

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

如果在EL表达式中使用立即求值而不是延迟求值- ${}而不是#{},也可能会发生这种情况。

顺便说一句,正如本文中所指出的,这种查找算法只有在组件标识符对于视图是唯一的时才会起作用;规范说它只需要对于NamingContainer__是唯一的;如果您在页面上使用唯一的组件it时小心一点,这不是问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1493217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档