【问题标题】:Retrieve client id for facelets检索 facelets 的客户端 ID
【发布时间】:2013-01-24 10:40:58
【问题描述】:

如何检索包含在 ui:include 中的 facelet 的 clientId?

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

EDIT1

问题是在Determine absolute id 的上下文中。要包含动态编辑器,我使用custom include

DynamicInclude、DynamicIncludeComponent 和 DynamicIncludeHandler 的源代码可以在:http://pastebin.com/5e2dgR15 找到。我必须删除在 DynamicInclude 的 getSrc 方法中测试 src 是否为空的行,并更改 getFamily 以返回非空值。这是我在我的案例中可以找到和使用的唯一动态包含的实现。在这一刻,我没有知识做一个更好的。拥有动态包含对我的项目至关重要,因为它在很多地方都被使用(@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 方法的第一个参数。

这些是我奇怪请求的原因。

【问题讨论】:

  • 我已经用具体的功能要求更新了我的问题。
  • 除了具体的问题,我需要先解决这个问题,这个组件是否可以在 MyFaces 中使用?
  • 我可以使用 <ui:param> 将所需的 id 传递给自定义包含,如下所示:<ui:param name="parentId" value="#{component.namingContainer.clientId}" />,但我在更新属性中使用此值时遇到问题。错误是:严重:javax.faces.FacesException:找不到标识符为“:form:tabs:1:editorsGroup:1:editor2:roles”的组件,引用自“form:tabs:1:editorsGroup:1:editor2:databases: 0:j_id1533934859_460afa94"。
  • 如果我删除命名容器添加到 id 的数字(例如:form:tabs:editorsGroup:editor2:roles),它会起作用。
  • 我正在使用 PrimeFaces 3.4.2、Spring Web Flow 2.3.1 和 JSF Mojarra 2.1.13。

标签: java jsf jsf-2 facelets composition


【解决方案1】:

我通过创建类似于#{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, "");
}

函数用作#{&lt;utils:absolute('componentId')&gt;}

【讨论】:

    猜你喜欢
    • 2022-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多