The <ui:repeat>
tag is what you should really use. The JSTL tags operate outside of the JSF Lifecycle. Cay Horstman has a JSF coursewhich discusses this fact: ui:repeat and Handling
Variable-Length Data.
There are a couple of solutions below which demonstrate some flexibility. You could do something like this:
<ui:param name="max" value="5"/>
<ui:repeat var="i" value="#{indexBean.values}" size="#{max}" >
<tr><td>#{i}</td></tr>
</ui:repeat>
The maximum number of rows is determined by a a <ui:parameter>
named max
. This is not required, but does demonstrate flexibility. Alternatively you could use something like:
<ui:param name="max" value="5"/>
<ui:repeat var="i" value="#{indexBean.rowNumbers(max)}">
<tr><td>#{i}</td></tr>
</ui:repeat>
The backing bean code is the following:
@ManagedBean
public class IndexBean {
public List<Integer> getValues() {
List<Integer> values = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
values.add(i);
}
return values;
}
public List<Integer> rowNumbers(final int max) {
List<Integer> values = new ArrayList<Integer>();
for (int i = 0; i < max; i++) {
values.add(i);
}
return values;
}
}
c:forEach
is okay for simple stuff like this. However, due to the rendering nature of it, you can easily get into trouble. I recommend usingui:repeat
instead. – Williemaewillies<ui:repeat>
takes a collection as a value and iterates over each element, inside you can write any markup you want. – Diley