Monday, July 09, 2012

JSF Tip of the Day: <ui:repeat /> Usage Example

Abstract

A common use case is to iterate over a collection of elements, and display them on a page. In the world of JSP, we would use a Java Standard Tag Library (JSTL) <c:forEach/>. This will work in JSF, but does have the same lifecycle as JSF which can cause issues if the data is dependent on the JSF lifecycle.
The solution is to use <ui:repeat/> to iterate over the collection. The <ui:repeat/> Facelets component is designed to work with JSF. It has the same lifecycle as the other JSF components on the page, and is a lot more flexible than the <c:forEach/> tag.

Description

The examples in this project demonstrate a number of methods to utilize the <ui:repeat/> in your own project.

Bug

There is a bug in versions of Mojarra less than 2.1.9 that the size includes the end element as per JAVASERVERFACES-2210.It was fixed as of version 2.1.9. Note: You will need to upgrade JSF libraries in GlassFish to see the corrections. Please see the release notes for Java Server Faces for upgrade procedures. The project was developed using NetBeans 7 IDE and Apache Maven. The project was tested on GlassFish 3.1.2.

The code for the project can be downloaded here: ui-repeat.zip

Code

index.xhtml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title><ui:repeat/> Examples</title>
    </h:head>
    <h:body>
        <h1>
<ui:repeat/> Examples</h1>
<h2>
Example 1</h2>
<p>
This example uses a <ui:repeat size="max" value="5"/> to set the number of times the rows repeat.
 
            Please note that the size includes the end element as per <a href="http://java.net/jira/browse/JAVASERVERFACES-2210">JAVASERVERFACES-2210</a>.
 
            It was fixed as of version 2.1.9 and now correctly displays 5 elements instead of 6.
        </p>
<p>
<strong>Note:</strong> You will need to upgrade JSF libraries in GlassFish to see the corrections.
 
            Please see the release notes for Java Server Faces for upgrade procedures.
        </p>
<ui:param name="max" value="5"/>
        <table>
            <ui:repeat var="i" size="#{max}" value="#{indexBean.values}">
<tr>
                    <td>
                        #{i}
                    </td>
                </tr>
</ui:repeat>
        </table>
<hr/>
        <h2>
Example 2</h2>
<p>
This example uses <ui:repeat /> that has the value
 
            bound using EL 2.2 in a <code>@ManagedBean</code> with <code>indexBean.rowNumbers(max)</code>.
        </p>
<table>
            <ui:repeat var="i" value="#{indexBean.rowNumbers(max)}">
<tr>
                    <td>
                        #{i}
                    </td>
                </tr>
</ui:repeat>
        </table>
<hr/>
        <h2>
Example 3</h2>
<p>
This example uses a size=10 which should correctly print out all 10 elements.
        </p>
<table>
            <ui:repeat var="i" size="10" value="#{indexBean.values}">
<tr>
                    <td>
                        #{i}
                    </td>
                </tr>
</ui:repeat>
        </table>
</h:body>
</html>

IndexBean.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.bluelotussoftware.example.jsf;
 
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
 
/**
 * Page backing bean used to provide sample values for the <ui:repeat/>
 * components on the index.xhtml page.
 *
 * @author John Yeary
 * @version 1.0
 */
@ManagedBean
public class IndexBean {
 
    /**
     * Generates a list of 10 values from 0-9.
     *
     * @return a list of values from 0-9.
     */
    public List<integer> getValues() {
        List<integer> values = new ArrayList<integer>();
        for (int i = 0; i < 10; i++) {
            values.add(i);
        }
        return values;
    }
 
    /**
     * Return a list of row numbers from 0 to the maximum value provided.
     *
     * @param max the maximum size of the list of returned values.
     * @return a list of values starting from 0 to {@code max}.
     */
    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;
    }
}

0 comments :

Popular Posts