Duplicate component ID in JSF using composite component twice in view

南楼画角 提交于 2019-12-05 11:12:45

You should not assign an id for your input text on your component.

If you are looping through an array on your form, it's certain that your input text element will be created more than once.

The specified identifier must be unique among all the components (including facets) that are descendents of the nearest ancestor UIComponent that is a NamingContainer, or within the scope of the entire component tree if there is no such ancestor that is a NamingContainer.

As stated on UINamingContainer.

Let's consider that your controller.allArgs has two items on list.

Your generated output:

<form id="anotherForm">
   <input type="text" id="**argValue**">
   <p:message>
   <input type="text" id="**argValue**">
   <p:message>
</form>

And this'll cause a duplicated id exception.

You could create a new naming container to keep your input id unique or append an index information on your add.

What you have right now is multiple JSF components with the same ID, which is not going to work..

When dynamically generating components you have to append some kind of an iteration index to the respective ids.

<p:inputText id="argValue_#{bean.counter}" value="#{cc.attrs.arg}" />
    <p:message id="argValueMessage_#{bean.counter}" for="argValue" />

The best thing you can do though is remove the id tag completely and let JSF auto-generate them.

If, of course you do not refence those ids from somewhere else.

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