- JSF 教程
- JSF - 首頁
- JSF - 概述
- JSF - 環境搭建
- JSF - 架構
- JSF - 生命週期
- JSF - 第一個應用程式
- JSF - 託管Bean
- JSF - 頁面導航
- JSF - 基本標籤
- JSF - Facelet標籤
- JSF - 轉換器標籤
- JSF - 驗證器標籤
- JSF - 資料表
- JSF - 複合元件
- JSF - Ajax
- JSF - 事件處理
- JSF - JDBC整合
- JSF - Spring整合
- JSF - 表示式語言
- JSF - 國際化
- JSF 有用資源
- JSF - 快速指南
- JSF - 有用資源
- JSF - 討論
JSF - 顯示資料表
h:dataTable 標籤用於以表格形式顯示資料。
JSF 標籤
<h:dataTable value = "#{userData.employees}" var = "employee"
styleClass = "employeeTable"
headerClass = "employeeTableHeader"
rowClasses = "employeeTableOddRow,employeeTableEvenRow">
<h:column>
<f:facet name = "header">Name</f:facet>
#{employee.name}
</h:column>
<h:column>
<f:facet name = "header">Department</f:facet>
#{employee.department}
</h:column>
<h:column>
<f:facet name = "header">Age</f:facet>
#{employee.age}
</h:column>
<h:column>
<f:facet name = "header">Salary</f:facet>
#{employee.salary}
</h:column>
</h:dataTable>
渲染輸出
<table class = "employeeTable">
<thead>
<tr>
<th class = "employeeTableHeader" scope = "col">Name</th>
<th class = "employeeTableHeader" scope = "col">Department</th>
<th class = "employeeTableHeader" scope = "col">Age</th>
<th class = "employeeTableHeader" scope = "col">Salary</th>
</tr>
</thead>
<tbody>
<tr class = "employeeTableOddRow">
<td>John</td>
<td>Marketing</td>
<td>30</td>
<td>2000.0</td>
</tr>
<tr class = "employeeTableEvenRow">
<td>Robert</td>
<td>Marketing</td>
<td>35</td>
<td>3000.0</td>
</tr>
</table>
標籤屬性
| 序號 | 屬性 & 描述 |
|---|---|
| 1 | id 元件識別符號 |
| 2 | rendered 布林值;false 抑制渲染 |
| 3 | dir 文字方向。有效值為 ltr(從左到右)和 rtl(從右到左) |
| 4 | styleClass 層疊樣式表 (CSS) 類名 |
| 5 | value 元件的值,通常是值繫結 |
| 6 | bgcolor 表格的背景顏色 |
| 7 | border 表格邊框的寬度 |
| 8 | cellpadding 表格單元格周圍的填充 |
| 9 | cellspacing 表格單元格之間的間距 |
| 10 | columnClasses 列的 CSS 類名的逗號分隔列表 |
| 11 | first 表格中顯示的第一行的索引 |
| 12 | footerClass 表格頁尾的 CSS 類 |
| 13 | frame 指定應繪製的表格周圍框架的邊;有效值:none、above、below、hsides、vsides、lhs、rhs、box、border |
| 14 | headerClass 表格表頭的 CSS 類 |
| 15 | rowClasses 行的 CSS 類名的逗號分隔列表 |
| 16 | rules 指定單元格之間繪製的線條;有效值:groups、rows、columns、all |
| 17 | summary 表格用途和結構的摘要,用於非視覺反饋,例如語音 |
| 18 | var 資料表建立的變數的名稱,表示 value 中的當前項 |
| 19 | title 標題,用於輔助功能,描述元素。視覺瀏覽器通常為 title 的值建立工具提示 |
| 20 | width 元素的寬度 |
| 21 | onblur 元素失去焦點 |
| 22 | onchange 元素的值更改 |
| 23 | onclick 滑鼠按鈕單擊元素 |
| 24 | ondblclick 滑鼠按鈕雙擊元素 |
| 25 | onfocus 元素獲得焦點 |
| 26 | onkeydown 按下按鍵 |
| 27 | onkeypress 按下按鍵然後釋放 |
| 28 | onkeyup 釋放按鍵 |
| 29 | onmousedown 滑鼠按鈕按下元素 |
| 30 | onmousemove 滑鼠移動到元素上 |
| 31 | onmouseout 滑鼠離開元素區域 |
| 32 | onmouseover 滑鼠移動到元素上 |
| 33 | onmouseup 滑鼠按鈕釋放 |
示例應用程式
讓我們建立一個測試 JSF 應用程式來測試上述標籤。
| 步驟 | 描述 |
|---|---|
| 1 | 在包 com.tutorialspoint.test 下建立一個名為 helloworld 的專案,如JSF - 基本標籤章節的JSF - h:outputStylesheet子章節中所述。 |
| 2 | 修改 styles.css,如下所示。 |
| 3 | 在包 com.tutorialspoint.test 下建立 Employee.java,如下所示。 |
| 4 | 建立 UserData.java 作為包 com.tutorialspoint.test 下的託管 Bean,如下所示。 |
| 5 | 修改 home.xhtml,如下所示。保持其餘檔案不變。 |
| 6 | 編譯並執行應用程式以確保業務邏輯按要求工作。 |
| 7 | 最後,將應用程式構建成 war 檔案,並將其部署到 Apache Tomcat Web 伺服器。 |
| 8 | 使用適當的 URL 啟動您的 Web 應用程式,如下面的最後一步所示。 |
styles.css
.employeeTable {
border-collapse:collapse;
border:1px solid #000000;
}
.employeeTableHeader {
text-align:center;
background:none repeat scroll 0 0 #B5B5B5;
border-bottom:1px solid #000000;
padding:2px;
}
.employeeTableOddRow {
text-align:center;
background:none repeat scroll 0 0 #FFFFFFF;
}
.employeeTableEvenRow {
text-align:center;
background:none repeat scroll 0 0 #D3D3D3;
}
Employee.java
package com.tutorialspoint.test;
public class Employee {
private String name;
private String department;
private int age;
private double salary;
private boolean canEdit;
public Employee (String name,String department,int age,double salary) {
this.name = name;
this.department = department;
this.age = age;
this.salary = salary;
canEdit = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public boolean isCanEdit() {
return canEdit;
}
public void setCanEdit(boolean canEdit) {
this.canEdit = canEdit;
}
}
UserData.java
package com.tutorialspoint.test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String dept;
private int age;
private double salary;
private static final ArrayList<Employee> employees
= new ArrayList<Employee>(Arrays.asList(
new Employee("John", "Marketing", 30,2000.00),
new Employee("Robert", "Marketing", 35,3000.00),
new Employee("Mark", "Sales", 25,2500.00),
new Employee("Chris", "Marketing", 33,2500.00),
new Employee("Peter", "Customer Care", 20,1500.00)
));
public ArrayList<Employee> getEmployees() {
return employees;
}
public String addEmployee() {
Employee employee = new Employee(name,dept,age,salary);
employees.add(employee);
return null;
}
public String deleteEmployee(Employee employee) {
employees.remove(employee);
return null;
}
public String editEmployee(Employee employee) {
employee.setCanEdit(true);
return null;
}
public String saveEmployees() {
//set "canEdit" of all employees to false
for (Employee employee : employees) {
employee.setCanEdit(false);
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
home.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!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:h = "http://java.sun.com/jsf/html"
xmlns:f = "http://java.sun.com/jsf/core">
<h:head>
<title>JSF tutorial</title>
<h:outputStylesheet library = "css" name = "styles.css" />
</h:head>
<h:body>
<h2>DataTable Example</h2>
<h:form>
<h:dataTable value = "#{userData.employees}" var = "employee"
styleClass = "employeeTable"
headerClass = "employeeTableHeader"
rowClasses = "employeeTableOddRow,employeeTableEvenRow">
<h:column>
<f:facet name = "header">Name</f:facet>
#{employee.name}
</h:column>
<h:column>
<f:facet name = "header">Department</f:facet>
#{employee.department}
</h:column>
<h:column>
<f:facet name = "header">Age</f:facet>
#{employee.age}
</h:column>
<h:column>
<f:facet name = "header">Salary</f:facet>
#{employee.salary}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
完成所有更改後,讓我們像在 JSF - 第一個應用程式章節中那樣編譯並執行應用程式。如果您的應用程式一切正常,這將產生以下結果。