获取 JSF2 复合组件的父组件的客户端标识



我有以下代码:

<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?

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

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

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

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

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

然后添加一个像

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

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

最新更新