How can I set id of a component/tag inside ui:repeat

放肆的年华 提交于 2019-11-26 22:55:11

This is not possible with a render-time tag such as <ui:repeat>. The <ui:repeat> will however by itself already ensure the uniqueness of the generated client ID by prepending it with the row index. So just remove the EL part from the ID attribute of the component.

<ui:repeat value="#{bean.columns}" var="column">
    <h:panelGroup layout="block" id="column">

With a view build time tag such as <c:forEach> (which will basically generate multiple <h:panelGroup> components instead of only one which is rendered multiple times), it is possible to specify a dynamic ID like that.

<c:forEach items="#{bean.columns}" var="column">
    <h:panelGroup layout="block" id="column_#{column.id}">

(you should only be well aware of how JSTL works in Facelets)

An alternative is to use a static <div> element instead of a JSF <h:panelGroup layout="block"> component.

<ui:repeat value="#{bean.columns}" var="column">
    <div id="column_#{column.id}">

See also:

JSF prefixes the id automatically. If you simply write id="column" the generated HTML will contain such identifiers:

myForm:0:column myForm:1:column myForm:2:column

and so on.

Anyway: Do never use JSTL tags (like c:foreach and c:if) in JSF templates. They cause random behaviour, very difficult to debug. And if they work, the slow down the application a lot.

Use ui:repeat for loops, and ui:fragment for conditional blocks. Note that there is no replacement for c:set, such a construct does not exist anymore in JSF 2.

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