【问题标题】:Omnifaces TagAttribute Id cannot be used as idOmnifaces TagAttribute Id 不能用作 id
【发布时间】:2021-07-27 12:16:34
【问题描述】:

我有以下 Facelet Taglib:

<ui:composition
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:o="http://omnifaces.org/ui"
        xmlns:h="http://java.sun.com/jsf/html">

    <o:tagAttribute name="id"/>

    <h:panelGroup id="#{id}" layout="block" styleClass="idTest">
        #{id}
    </h:panelGroup>
</ui:composition>

taglib.xml 如下所示:

<tag>
    <tag-name>idTest</tag-name>
    <source>resources/myProject/tags/idTest.xhtml</source>
</tag>

而且使用它的代码很简单:

<myProject:idTest/>

怎么可能会呈现以下 HTML:

<div class="idTest">
    j_ido489794984_4bf870cd
</div>

为什么我的PanelGroup 没有id? id 是根据 o:tagAttribute 的文档生成的,因为 div 的内容已被渲染。但是作为id 它不起作用。为什么?

【问题讨论】:

    标签: jsf omnifaces


    【解决方案1】:

    这确实令人困惑。

    documentation 字面意思是:

    ...它将自动生成j_ido[tagId] 形式的唯一ID,其中[tagId] 是&lt;o:tagAttribute&gt; 标签自己的唯一ID。

    但实际行为更像这样:

    ...它会将任何自动生成的 ID 覆盖为 j_ido[tagId] 的形式,其中 [tagId] 是 &lt;o:tagAttribute&gt; 标记自己的唯一 ID。

    换句话说,当 JSF 本身需要渲染 HTML 元素的 id 属性时,通常是因为链中的某些内部逻辑需要它,例如 &lt;f:ajax&gt; 和朋友,并且没有像&lt;x:someTag id="fixedId" /&gt;这样的标签上指定明确的ID,那么JSF默认会自动生成j_id[autoIncrementInteger]的形式。但这在标记文件上会出错,因为 autoIncrementInteger 在每次回发时可能会进一步增加一个,具体取决于所使用的 JSF impl 和视图状态配置。 &lt;o:tagAttribute&gt; 只是尝试以这种方式确保自动生成的 ID 在每次回发时保持相同。

    当您编辑测试标记文件以添加 &lt;f:ajax&gt;,

    <h:panelGroup id="#{id}" layout="block" styleClass="idTest">
        <f:ajax event="click" />
    </h:panelGroup>
    

    然后你会看到生成的&lt;div&gt; 有一个id,因为这在技术上是&lt;f:ajax&gt; 所要求的。

    <div id="j_ido-1512689859_13a7e7e3" class="idTest"
        onclick="mojarra.ab(this,event,'click',0,0)">
    </div>
    

    或者当您将&lt;h:panelGroup&gt; 换成例如&lt;h:form&gt; 或任何在客户端始终需要 ID 的组件,

    <h:form id="#{id}" styleClass="idTest">
        <ui:insert />
    </h:form>
    

    然后你也会看到它已经生成了。

    <form id="j_ido-1512689859_13a7e7e3" name="j_ido-1512689859_13a7e7e3" method="post" action="/test.xhtml" class="idTest" enctype="application/x-www-form-urlencoded">
        <input type="hidden" name="j_ido-1512689859_13a7e7e3" value="j_ido-1512689859_13a7e7e3" />
        ...
    </form>
    

    换句话说,该功能运行良好,但在您的特定情况下根本没有使用它,因为 JSF 认为没有必要生成它。

    我同时updated 文档。

    【讨论】:

    • 感谢您的解释。如果没有 f:ajax,我怎么能欺骗 JSF 为我的 div 生成一个 id?你有什么想法吗?
    • 你为什么想要一个显然不会在任何地方使用的 ID?
    猜你喜欢
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多