如何获取数据表中组件的jsf clientid?

发布于 2024-08-06 07:35:08 字数 1782 浏览 6 评论 0原文

我正在尝试获取数据表中组件的客户端 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:// invalidargumentexception.blogspot.com/2009/05/jsf-using-component-ids-in-data-table.html

但是我打算有一个更简单的实现,而不是像作者那样的 TLD 函数或facelets。

有人有什么想法吗?

谢谢,

dzh

i am trying to get the client id of a component in a datatable. the problem is that jsf puts row index before the component id automatically, i.e.

   <a id="mappedidentifier_table:1:mappedidentifier_Update" href="#">Update</a>

for a link in the second row (index=1).

i am using the following methods to get the 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;
    }

however this returns

mappedidentifier_table:mappedidentifier_Update,

instead of

mappedidentifier_table:1:mappedidentifier_Update,

therefore it does not match any element cause the row index in id is missing.

i've read http://illegalargumentexception.blogspot.com/2009/05/jsf-using-component-ids-in-data-table.html

however i intend to have a simpler implementation, rather than TLD function or facelets like the author did.

does anyone have any thoughts ?

thanks,

dzh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-08-13 07:35:08

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

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

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

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

however this returns mappedidentifier_table:mappedidentifier_Update instead of mappedidentifier_table:1:mappedidentifier_Update

This will happen if you resolve the clientId outside the context of a row. The row context is set up at each stage of the lifecycle by the UIData control.

It might also happen if you're using immediate evaluation instead of deferred evaluation in your EL expressions - ${} instead of #{}.

As an aside, and as noted in the article, that lookup algorithm will only work if the component identifier is unique to the view; the spec says it need only be unique to the NamingContainer; this need not be a problem if you are careful about using unique component IDs on your page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文