JSF 2.2: CollectionDataModel

In my last post of the series on JSF 2.2 features I presented view actions, a feature that was missing since the introduction of HTTP GET support in JSF 2.0. Another feature that has been missing even longer is the CollectionDataModel.

Before JSF 2.2, the list of types supported by h:dataTable out of the box is quite limited. In my experience it basically boils down to java.util.List, arrays and javax.faces.model.DataModel (for paging and sorting in DB). It is for instance not possible to use a property of type java.util.Set directly in the value attribute of h:dataTable.

This changes with JSF 2.2 and the new CollectionDataModel: h:dataTable now works with all collections implementing java.util.Collection.

Let’s have a look at a small example. The following listing shows the data table from the person list view introduced in the last post:

<h:dataTable value="#{personPage.persons}" var="p">
  <h:column>
    <f:facet name="header">Person</f:facet>
    <h:link value="#{p.name}" outcome="personDetails">
      <f:param name="id" value="#{p.id}"/>
    </h:link>
  </h:column>
</h:dataTable>

The data for h:dataTable is coming from the property personPage.persons. As you can see in the following listing, getPersons() has a return type of Collection and not List:

@ManagedBean 
@RequestScoped
public class PersonPage {
  @ManagedProperty("#{personRepository}")
  private PersonRepository personRepository;

  public Collection<Person> getPersons() {
    return personRepository.getPersons();
  }
  // Additional code...
}

The complete list of supported types for h:dataTable in JSF 2.2 looks like this:

  1. null will become the empty list.
  2. javax.faces.model.DataModel
  3. Arrays
  4. java.util.List
  5. java.sql.ResultSet
  6. javax.servlet.jsp.jstl.sql.Result
  7. java.util.Collection (new in JSF 2.2)
  8. All other types will be handles as scalar value.

The order of the above list of types is important. In my example I had to add a dependency to the JSTL as JSF first checks for the JSTL Result type before the CollectionDataModel is used.

The source code for the JSF 2.2 series examples can be found in the JSFlive Github repository jsf22-examples (module jsf22-collection-data-model).

Further official details about JSF 2.2 can be found in the JSR 344: JavaServer Faces 2.2.

Advertisements

3 responses to “JSF 2.2: CollectionDataModel

  1. Pingback: JSF 2.2 - CollectionDataModel - Blog - Irian

  2. Pingback: JSFlive: Michael Kurz’s JSF Weblog | java web development

  3. Pingback: description_news_of_jsf2.2_and_samples_jsf2.2 | mauroprogram's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s