h:dataTable è un tag per visualizzare una lista di oggetti in una tabella HTML.
Creare una classe User che contiene le informazioni da visualizzare:
User.java
public class User {
private String name;
private String surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
Creare un backing bean
TableExampleBean
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class TableExampleBean {
private List<User> users;
@PostConstruct
public void init() {
users = new ArrayList<User>();
User user = new User();
user.setName(“Paperino”);
user.setSurname(“Rossi”);
users.add(user);
user = new User();
user.setName(“Pippo”);
user.setSurname(“Rossi”);
users.add(user);
user = new User();
user.setName(“Pluto”);
user.setSurname(“Rossi”);
users.add(user);
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
Creare la pagina xhtml contenente il tag
showUsers.xhtml
<html xmlns=”http://www.w3.org/1999/xhtml”
xmlns:f=”http://java.sun.com/jsf/core”
xmlns:h=”http://java.sun.com/jsf/html”
xmlns:ui=”http://java.sun.com/jsf/facelets”
xmlns:p=”http://primefaces.org/ui”
xmlns:c=”http://java.sun.com/jsp/jstl/core”>
<h:head>
</h:head>
<h:body>
<h:form>
<h:dataTable var=”user” value=”#{tableExampleBean.users}”>
<h:column>
<h:outputText value=”#{user.name}” />
</h:column>
<h:column>
<h:outputText value=”#{user.surname}” />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
Output visualizzato: