简体   繁体   English

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

[英]how to get jsf clientid of a component in datatable?

i am trying to get the client id of a component in a datatable. 我试图获取数据表中的组件的客户端ID。 the problem is that jsf puts row index before the component id automatically, ie 问题是jsf会自动将行索引放在组件id之前,即

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

for a link in the second row (index=1). 对于第二行中的链接(index = 1)。

i am using the following methods to get the clientId 我使用以下方法来获取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 , mappedidentifier_table:mappedidentifier_Update

instead of 代替

mappedidentifier_table:1:mappedidentifier_Update , mappedidentifier_table:1:mappedidentifier_Update

therefore it does not match any element cause the row index in id is missing. 因此它与任何元素都不匹配导致id中的行索引丢失。

i've read http://illegalargumentexception.blogspot.com/2009/05/jsf-using-component-ids-in-data-table.html 我读过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. 但是我打算有一个更简单的实现,而不是像作者那样的TLD功能或facelets。

does anyone have any thoughts ? 有人有想法吗 ?

thanks, 谢谢,

dzh DZH

however this returns mappedidentifier_table:mappedidentifier_Update instead of mappedidentifier_table:1:mappedidentifier_Update 但是这会返回mappedidentifier_table:mappedidentifier_Update而不是mappedidentifier_table:1:mappedidentifier_Update

This will happen if you resolve the clientId outside the context of a row. 如果您在行的上下文之外解析clientId则会发生这种情况。 The row context is set up at each stage of the lifecycle by the UIData control. 行上下文由生命周期的每个阶段由UIData控件设置。

It might also happen if you're using immediate evaluation instead of deferred evaluation in your EL expressions - ${} instead of #{} . 如果您在EL表达式中使用即时评估而不是延迟评估,则可能会发生这种情况 - ${}而不是#{}

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 ; 规范说它只需要NamingContainer是唯一的; this need not be a problem if you are careful about using unique component IDs on your page. 如果您在页面上使用唯一的组件ID时要小心,这不一定是个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM