【问题标题】:Composite components & ID复合组件和 ID
【发布时间】:2012-05-08 13:51:12
【问题描述】:

我想在我的 JSF 复合组件中实现一些 javas 脚本,但是我的 id 有问题。我的 java 脚本:

document.getElementById("myForm:customerId")

不起作用,因为 id 错误。我有 JSF 复合组件:

<composite:implementation>
    <div id="element_customer">
        <h2 class="element_title">Customer</h2>
        <h:form id="myForm">
            <h:inputText id="customerId" value="#{cc.attrs.customerId}"/>
        </h:form>
    </div>
</composite:implementation>

HTML 输出为:

<div id="element_customer">
    <h2 class="element_title">Customer</h2>
    <form id="j_idt44:myForm" name="j_idt44:myForm" method="post" ... >
        <input type="hidden" name="j_idt44:myForm" value="j_idt44:myForm" />
        <input id="j_idt44:myForm:customerId" ... name="j_idt44:myForm:customerId" />
    </form>
 </div>

为什么在 HTML 输出中使用“j_idt44”?

【问题讨论】:

    标签: jsf jsf-2 composite-component


    【解决方案1】:

    复合组件是NamingContainer 组件,如&lt;h:form&gt;、&lt;h:dataTable&gt; 等。这允许您在同一个视图中拥有多个它们而不会出现ID 冲突。

    您还需要为复合组件指定一个固定 ID。例如

    <my:composite id="someId" />
    

    我还建议使用&lt;div id="#{cc.id}"&gt; 而不是&lt;div id="element_customer"&gt;。然后它将在上面的示例中变为someId。


    与具体问题无关,这并不完全是复合组件的正确用途。复合组件旨在与&lt;h:inputText&gt; 等类型相同。您似乎更想要一个标记文件或者可能是一个包含文件。另见When to use <ui:include>, tag files, composite components and/or custom components?

    【讨论】:

    猜你喜欢
    • 2011-06-04
    • 2018-07-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 2013-12-16
    • 2011-12-19
    相关资源
    最近更新 更多