【问题标题】:Obtaining the clientId of the parent of a JSF2 composite component获取JSF2复合组件父级的clientId
【发布时间】:2012-09-10 18:00:56
【问题描述】:

我有以下代码:

<h:dataTable id="dt" value="#{somelist}" var="entry">
    <h:column>
        #{entry.title}
    </h:column>
    <h:column>
        <h:commandLink id="lnk">
           <mycomp:doSomething id="dummy" />
        </h:commandLink>
    </h:column>
</h:dataTable>

我的复合组件 (mycomp:doSomething) 如下所示:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:composite="http://java.sun.com/jsf/composite">

    <composite:interface>
    </composite:interface>

    <composite:implementation >     
        <script type="text/javascript">
            // #{component.parent.clientId}
        </script>        
    </composite:implementation>
</html>

我希望输出 (#{component.parent.clientId}) 类似于以下内容:dt:0:lnk,但它返回的是 dt:0:dummy,即复合组件的客户端 ID。

如何获取真实父标签的ID?

【问题讨论】:

    标签: jsf-2 composite-component


    【解决方案1】:

    请改用 #{cc.parent.clientId}。 Composite:implementation 中的所有内容都在 UIPanel 中,该 UIPanel 位于复合组件基础实例(通常是 NamingContainer)内的 facelet 上。

    更新:检查代码,cc.parent 解析父复合组件而不是直接父组件。这似乎是一个旧的实现细节,但规范中没有提到。此答案中提出的解决方案不起作用:(。

    http://lists.jboss.org/pipermail/jsr-314-open-mirror/2010-February/002474.html

    可以绕过 cc.parent 的解析,提供自定义组件类扩展 UINamingContainer 并添加:

    <composite:interface componentType="my.custom.ComponentBaseClass">
    

    然后添加一个像这样的getter

    public UIComponent getImmediateParent()
    {
         return getParent();
    }
    

    最后使用#{cc.immediateParent.clientId}。它应该以这种方式工作。

    【讨论】:

    • #{cc.parent.clientId} 返回一个空字符串(或不返回任何内容)
    • #{cc.parent.clientId} 如果外部组件(父级)也是复合组件,则有效。但是如果它是像我上面的例子一样的 ,它不会返回任何东西,实际上 cc.parent 返回 null。
    • 它的工作原理是像您描述的那样为组件实现 UINamingContainer 。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 2011-11-10
    • 2011-09-06
    • 2017-01-04
    • 1970-01-01
    • 2014-01-09
    • 2012-12-07
    • 2012-04-10
    相关资源
    最近更新 更多