- 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 - 第一个应用程序章节中那样编译并运行应用程序。如果您的应用程序一切正常,这将产生以下结果。