"for" cycle in JSF
Asked Answered
H

5

8

I simply need to perform a very basic for cycle in JSF/ICEFaces, basically rendering column numbers

Something like the following pseudo-code

for(int i=0; i<max; i++)
{
   <td>#{i}</td>
}

the <c:forEach> tag iterates over collections, but I don't want to make my backing bean more complex returning a stupid collection of integers.

Do you know a shorter and smarter way?

Thank you

Humorous answered 10/3, 2011 at 10:0 Comment(2)
JSTL's 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 using ui:repeat instead.Williemaewillies
I second @geca's reply. <ui:repeat> takes a collection as a value and iterates over each element, inside you can write any markup you want.Diley
B
5

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;
}
}
Broz answered 14/5, 2012 at 14:51 Comment(2)
Please note I have opened a bug JAVASERVERFACES-2425 on the size attribute. The size VDL document does not indicate that the value includes the end element for example size=5 prints out 0-5 instead of 0-4 which would be five elements. I expect this will be corrected in 2.2.Broz
The behavior is fixed in 2.1.9+Broz
O
12
<c:forEach var="i" begin="1" end="#{someBean.max}">
             <td>#{i}</td>      
 </c:forEach>
Ora answered 10/3, 2011 at 10:6 Comment(1)
Yea definitely correct ;);) I thought the items property was required, I just tried the code and worked fine :D :DNaoise
B
5

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;
}
}
Broz answered 14/5, 2012 at 14:51 Comment(2)
Please note I have opened a bug JAVASERVERFACES-2425 on the size attribute. The size VDL document does not indicate that the value includes the end element for example size=5 prints out 0-5 instead of 0-4 which would be five elements. I expect this will be corrected in 2.2.Broz
The behavior is fixed in 2.1.9+Broz
A
4

I suggest thinking at a higher level of abstraction, not in terms of rendering HTML tags, but in terms of using a component that does what you need. For example, Primefaces' datatable supports dynamic columns, which should be capable of replacing your on-page iteration logic.

Attraction answered 10/3, 2011 at 10:7 Comment(1)
Surely a wise answer, but what I need to do in my case is really too simple to think about a smarter design. Anyway thanks and upvote!Naoise
D
1

Simple example using ui repeat

<ul>
    <ui:repeat var="entry" value="${tourBean.tour.highlights}">
        <li class="pb-1">#{entry}</li>
    </ui:repeat>
</ul>
Dunant answered 12/9, 2017 at 21:52 Comment(0)
G
-1

with rich faces data table you can dynamically generate columns headers and values

Godiva answered 1/10, 2011 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.