Composite components & ID

萝らか妹 提交于 2019-12-08 17:01:13

问题


I want to implement some javas cript into my JSF composite component, but I have problem with id. My java script with:

document.getElementById("myForm:customerId")

does not work, because the id is wrong. I have JSF composite component:

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

and HTML output is:

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

Why is "j_idt44" used in HTML output?


回答1:


Composite components are NamingContainer components like <h:form>, <h:dataTable>, etc. This allows you to have multiple of them in the same view without conflicting IDs.

You need to give the composite component a fixed ID as well. E.g.

<my:composite id="someId" />

I'd also suggest to use <div id="#{cc.id}"> instead of <div id="element_customer">. It will then become someId with the above example.


Unrelated to the concrete problem, this isn't entirely the right purpose of a composite component. A composite component is intented to be of the same kind of <h:inputText>, etc. You seem to rather want a tag file or maybe an include file. See also When to use <ui:include>, tag files, composite components and/or custom components?



来源:https://stackoverflow.com/questions/10356993/composite-components-id

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