[wp_ad_camp_5]
This post shows a sample usage of JSF
Data Table
to construct a basic HTML
on a page. You can download the source codes from the GitHub
link provided at the very bottom of this page.
Requirements
Stuff used in this post.
- JSF 2.2
- javax.faces-2.2.8.jar
- JDK 8
JSF Managed Bean
The class only has a list of EmployeeDTO
objects returned to the JSF
framework.
[wp_ad_camp_4]
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 | package com.turreta.jsf.bean; import java.util.ArrayList; import java.util.List; import javax.faces.bean.ManagedBean; @ManagedBean @ApplicationScoped public class EmployeeListFormBean { private List<EmployeeDTO> employeeList = new ArrayList<>(); public EmployeeListFormBean() { // Simulates loading of data from some data store, e.g., database employeeList.add(new EmployeeDTO("Monroe", "Marilyn", "Human Resource")); employeeList.add(new EmployeeDTO("Lincoln", "Abraham", "Human Resource")); employeeList.add(new EmployeeDTO("Mandela", "Nelson", "Marketing")); employeeList.add(new EmployeeDTO("Orwell", "George", "IT")); } public List<EmployeeDTO> getEmployeeList() { return employeeList; } public void setEmployeeList(List<EmployeeDTO> employeeList) { this.employeeList = employeeList; } } |
And our EmployeeDTO
class:
[wp_ad_camp_3]
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 | package com.turreta.jsf.bean; public class EmployeeDTO { public EmployeeDTO(String lastName, String firstName, String deptName) { super(); this.lastName = lastName; this.firstName = firstName; this.deptName = deptName; } private String lastName; private String firstName; // Department name private String deptName; public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } } |
XHTML Page
We will only use on page that displays a list of employees when a URL
is accessed.
[wp_ad_camp_2]
Download the codes
[wp_ad_camp_1]
https://github.com/Turreta/JSF2-examples/tree/master/JSF2%20Data%20Table