31

Does the <ui:repeat /> tag support iterating over a java.util.Set? I've tried iterating over my JPA domain entity objects contained in a Set, but receive errors. Is there something I'm missing? Does an additional flag need to be present or something?

alexander
  • 1,191
  • 2
  • 20
  • 40
Shadowman
  • 11,150
  • 19
  • 100
  • 198

3 Answers3

76

The easiest way to finish the deal at page without modifying the class is converting the set to an array like this.

<ui:repeat value="#{myBean.mySet.toArray()}" var="_myvar">
prageeth
  • 7,159
  • 7
  • 44
  • 72
39

No, the ui:repeat does not support Set, nor does h:dataTable.

You should return a List from the Set, and use that instead.

public List<T> getListFromSet(Set<T> set) {
  return new ArrayList<T>(set);
}

You should avoid using c:forEach; here is an article on why.

Roger Keays
  • 3,117
  • 1
  • 31
  • 23
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
  • 2
    Since getters in JSF are usally called [several times](https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times) this can be an expensive solution. – Monkey Supersonic May 22 '17 at 14:45
0

Consider using c:forEach instead. It appears that ui:repeat does not support sets (i.e. requires some sort of ordering property).

Otherwise you can create your own tag as described in: http://techblog.bozho.net/?p=28

AdamH
  • 1,378
  • 9
  • 10
  • 6
    Do **NOT** consider using `c:forEach`: http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets – Shervin Asgari Oct 25 '10 at 18:54
  • 1
    It completely depends on what the set is that you are iterating over and if it compile time or not. So if that is the case then you CAN use `c:forEach`. – AdamH Oct 25 '10 at 18:57
  • Yes if you know what you are doing, then you can use `c:forEach`, but I read your post as that `you should consider using c:forEach`, and this is not the case. Better using `ui:repeat` to avoid problems – Shervin Asgari Oct 25 '10 at 19:01
  • @Shervin updated link: https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat – Roger Keays Feb 26 '21 at 15:08