Obtaining the clientId of the parent of a JSF2 composite component

梦想与她 提交于 2019-12-02 09:26:29

问题


I've got the following code:

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

My composite component (mycomp:doSomething) looks like this:

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

I would expect the output (#{component.parent.clientId}) to be something similar to this: dt:0:lnk but instead it returns dt:0:dummy i.e. the client ID of the composite component.

How do I get the ID of the real parent tag?


回答1:


Use #{cc.parent.clientId} instead. All content inside composite:implementation is inside a UIPanel that is on a facelet inside the composite component base instance, which usually is a NamingContainer.

UPDATE: Checking the code, cc.parent resolves the parent composite component instead of the immediate parent. It seems to be an old implementation detail, but it is not mentioned in the spec. The solution proposed in this answer does not work :(.

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

You can bypass the resolution of cc.parent, providing a custom component class extending UINamingContainer and adding this:

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

then add a getter like

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

and finally use #{cc.immediateParent.clientId}. It should work in this way.



来源:https://stackoverflow.com/questions/12477578/obtaining-the-clientid-of-the-parent-of-a-jsf2-composite-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!