一、标签简介
1.1 概念
JSF允许使用h:dataTable标签来放置组件,利用迭代器原理,遍历数据来创建html表格。
1.2 适用条件
遍历的内容应为以下类型的数据:
java对象
数组
java.util.List实例
java.sql.ResultSet实例
javax.servlet.jsp.jstl.sql.Result实例
javax.faces.model.DataModel实例
1.3 基本形式
< h:dataTable value ="" var ="" >
< h:column ></ h:column >
</ h:dataTable >
h:dataTable标签的正文体只包含h:column标签,除了可
选的标题和脚注组件之外,每列都可以包含无数的组件。任何时候都可以在有自组建的组件内部指定模板文件(即不是JSF标签的其他东西),但必须将这些模板文本封装到f:verbatim标签的正文体内或者使h:outputText来产生该模板文本。
常用的显示数据信息的方式有:
1.
< h:dataTable value ="" var ="" >
< h:column >
<f:facet name = ""></f:facet>
#{}
</ h:column >
</ h:dataTable >
2.
< h:dataTable value ="" var ="" >
< h:column >
<h:outputText value="#{}" />
</ h:column >
</ h:dataTable >
1.4 相关参数
- value 遍历对象,可以是数组、List集合等。
- var value中数组对象、List对象。
(说不太明白)
二、代码示例
下面以遍历List集合为例子展示:(不太好描述,就借用了别人的代码)
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: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>
三、易错点总结
1.var
var参数是value中List的一个实例化对象名;
2.生命周期问题
目前RequestScoped、SessionScoped两种生命周期可以实现正确显示。