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

Daniel picture Daniel · Feb 5, 2012 · Viewed 15.7k times · Source

I'm trying to assign an id to a component inside a <ui:repeat> like that:

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

The thing is that #{column.id} value is being placed properly inside the styleClass value but its not being set inside the id attribute. All that is being set inside the id attribute is the automatically generated id by the JSF + my hard coded value column_.

If I remove the hard coded column_ I get an exception:

java.lang.IllegalArgumentException: component identifier must not be a zero-length String at

Any Ideas?

Answer

BalusC picture BalusC · Feb 5, 2012

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: