Retrieve client id for facelets

纵然是瞬间 提交于 2019-12-25 01:35:53

问题


How do I retrieve clientId for a facelet included with ui:include?

For a reusable component I use this syntax: cc.clientId.

EDIT1

The question is in the context of Determine absolute id. To include the dynamic editors I use a custom include.

The source code of DynamicInclude, DynamicIncludeComponent and DynamicIncludeHandler can be found at: http://pastebin.com/5e2dgR15. I had to remove the lines that tested the src for null in getSrc method of DynamicInclude and changed getFamily to return a not null value. It is the only implementation of a dynamic include that I could find and use in my case. At this moment, I don't have the knowledge to make a better one. Having a dynamic include is crucial to my project because it is used in lots of places(@BalusC: I would love to see such component being add in OmniFaces if it's possible).

My problems with the absolute client id are related with the way the id gets generated for the <custom:include>. In my case is tabs:0:editorsGroup:4:editor3. I have seen that naming containers, such as <p:dataTable> or <p:tabView>, add a number to the id(tabs:0, editorsGroup:4). I am not sure if this custom include is 100 % a NamingContainer. DynamicIncludeComponent implements NamingContainer, but I cannot use an absolute client id as :tabs:editorsGroup:editor.

For the organizationUnit editor from Determine absolute id, I have used a workaround in the update for absolute_id_of_organization_unit. With #{eval.getAbsoluteId(cc.clientId, 'organizationUnit'), the absolute client id was calculated, after some parts of the cc.clientId have been removed.

I have tried to do the update with the help of a <p:remoteCommand>, but it didn't worked. So I have thought I could do a similar workaround as for the organizationUnit editor. For this I had to obtain the parent id, the first parameter for the getAbsoluteId method.

These are the reasons from my strange request.


回答1:


I have solved the issue by creating a function similar to #{p:component(componentId)}. It addition to returning the client id, it also removes the row index information from the generated client id.

The function is defined in WEB-INF/utils like this:

... doctype ommited
<facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
    <namespace>geneous.client.jsf/utils</namespace>
    <function>
        <function-name>absolute</function-name>
        <function-class>com.acme.util.ComponentUtils</function-class>
        <function-signature>java.lang.String getAbsoluteClientId(java.lang.String)</function-signature>
    </function>
</facelet-taglib>

Inside web.xml

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/utils/utils.taglib.xml</param-value>
</context-param>

Sample code from function:

public static String getAbsoluteClientId(String id) {
    final String clientId = removeRowIndexFromClientId(id);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    final char separator = UINamingContainer.getSeparatorChar(facesContext);        
    StringBuilder idBuilder = new StringBuilder();
    idBuilder.append(separator).append(clientId);
    return idBuilder.toString();        
}

public static String removeRowIndexFromClientId(String id) {
    String clientId = findComponentClientId(id);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    final char separator = UINamingContainer.getSeparatorChar(facesContext);
    final String regex = String.valueOf(separator) + "[0-9]+";
    return clientId.replaceAll(regex, "");
}

The function is used as #{<utils:absolute('componentId')>}.



来源:https://stackoverflow.com/questions/14776402/retrieve-client-id-for-facelets

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