12

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Daniel
  • 36,833
  • 10
  • 119
  • 200

2 Answers2

16

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:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Thanks, I eventually did it with div. So it mean that I cannot assign dynamic ID to JSF elements inside ui:repeat by myself? only to simple HTML elements – Daniel Feb 05 '12 at 11:59
  • 1
    That's correct. Render-time IDs cannot be registered in the component tree, it has to be a fixed value instead of a dynamic value. Whatever functional requirement you're thinking to solve by approaching it like this, it would not be the "JSF-ish" way and you might need to think twice about this to avoid future surprises. – BalusC Feb 05 '12 at 12:07
8

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.

Stefan
  • 117
  • 1
  • 2
  • Worked like a charm and it was a great explanation. I'm also wondering why it got a downvote, but I'll upvote since it makes a lot of sense. – Bruno Gasparotto Aug 21 '15 at 21:04
  • 1
    Downvote is because of "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." which is simply not true – Kukeltje Apr 12 '18 at 07:57
  • +1 for the contrary of the reason of the comment above. For example `c:foreach`, unline `ui:repeat`, is at compile time, and does not update with an ajax request. – Marco Sulla Jul 14 '21 at 08:39