Getting the clientId of the parent component of the JSF2

I have 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: dt:0:lnk , but instead it returns dt:0:dummy , that is, the client identifier of the composite component.

How to get the id of the real parent tag?

+1
source share
1 answer

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

UPDATE:. By checking the code, cc.parent resolves the parent component instead of the immediate parent. This seems to be an old implementation detail, but it is not mentioned in the specification. The solution suggested in this answer does not work :(.

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

You can get around the cc.parent permission by providing a custom component class that extends the UINamingContainer and adding the following:

 <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 this way.

+3
source

Source: https://habr.com/ru/post/1313366/


All Articles