UIData improvements

In JSF 2.3, <h:dataTable/> and <ui:repeat /> support Iterable and Map.

Iterable example

Let's create a simple demo to try it.

The backing bean.

@Model
public class IterableBean {

    @Inject Logger LOG;

    private Iterable data = Arrays.asList("javaee 8", "jsf 2.3");

    public Iterable getData() {
        LOG.log(Level.INFO, "called IterableBean.getData");
        return data;
    }

}

And the facelets template.

Map example

The backing bean.

And the facelets template.

Iteration without backing data

In former versions, when you want to iterate a number collection from 0 to 10, you have to use c:forEach which from legacy JSP taglibs.

Now the new ui:repeat added this feature finally.

Custom DataModel

JSF 2.3 provides a new @FacesDataModel to simplify the customization of your own DataModel.

The backing bean.

UserData is just a POJO which wraps a list of User.

The facelets template.

We use UserData as UIRepeat backing data, it is not supported in JSF by default. Let's fill the gap via custom @FacesDataModel class.

Run this application on Glassfish, open your browser and navigate to http://localhost:8080/jsf-data-model/custom-datamodel.faces.

custom datamodel

Grab the source codes from my GitHub account, and have a try.

Last updated

Was this helpful?