检索facelets的客户端ID

时间:2013-02-08 15:54:13

标签: java jsf jsf-2 facelets composition

如何检索ui:include?附带的facelet的clientId

对于可重用的组件,我使用以下语法:cc.clientId

EDIT1

问题出在Determine absolute id的背景下。要包含动态编辑器,我使用custom include

可以在http://pastebin.com/5e2dgR15找到DynamicInclude,DynamicIncludeComponent和DynamicIncludeHandler的源代码。我必须在DynamicInclude的getSrc方法中删除测试src为null的行,并更改getFamily以返回非null值。它是我能在我的案例中找到并使用的动态包含的唯一实现。此刻,我没有做出更好的知识。拥有动态包含对我的项目至关重要,因为它在很多地方使用(@BalusC:我很乐意看到这样的组件在OmniFaces中添加,如果可能的话)。

我对绝对客户端ID的问题与为<custom:include>生成id的方式有关。在我的情况下是tabs:0:editorsGroup:4:editor3。我已经看到命名容器(例如<p:dataTable><p:tabView>)在id中添加一个数字(tabs:0,editorsGroup:4)。我不确定这个自定义包含是否是100%的NamingContainer。 DynamicIncludeComponent实现了NamingContainer,但我不能将绝对客户端ID用作:tabs:editorsGroup:editor

对于Determine absolute idorganizationUnit编辑器,我在absolute_id_of_organization_unit的更新中使用了一种解决方法。使用#{eval.getAbsoluteId(cc.clientId, 'organizationUnit'),在删除cc.clientId的某些部分后,计算绝对客户端ID。

我试图在<p:remoteCommand>的帮助下进行更新,但它没有用。所以我认为我可以像organizationUnit编辑器那样做一个类似的解决方法。为此我必须获取父id,getAbsoluteId方法的第一个参数。

这是我奇怪的要求的原因。

1 个答案:

答案 0 :(得分:0)

我通过创建类似于#{p:component(componentId)}的函数解决了这个问题。除了返回客户端ID之外,它还从生成的客户端ID中删除行索引信息。

该功能在WEB-INF/utils中定义如下:

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

内部web.xml

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

来自函数的示例代码:

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, "");
}

该功能用作#{<utils:absolute('componentId')>}

相关问题