h:dataTable JSF2

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

package com;
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

package com;

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

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<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:

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *