以下代码显示如何向DataTable添加行号。
以下代码来自demo.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" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> <h:outputStylesheet library="css" name="table-style.css" /> </h:head> <h:body> <h:form> <h:dataTable value="#{book.bookList}" var="o" styleClass="book-table" headerClass="book-table-header" rowClasses="book-table-odd-row,book-table-even-row"> <h:column> <f:facet name="header">No</f:facet> #{book.bookList.rowIndex + 1} </h:column> <h:column> <f:facet name="header">Book No</f:facet>#{o.bookNo} </h:column> <h:column> <f:facet name="header">Product Name</f:facet>#{o.productName} </h:column> <h:column> <f:facet name="header">Price</f:facet>#{o.price} </h:column> <h:column> <f:facet name="header">Quantity</f:facet>#{o.qty} </h:column> </h:dataTable> <h3>Enter Book</h3> <table> <tr> <td>Book No :</td> <td><h:inputText size="20" value="#{book.bookNo}" /></td> </tr> <tr> <td>Product Name :</td> <td><h:inputText size="20" value="#{book.productName}" /></td> </tr> <tr> <td>Quantity :</td> <td><h:inputText size="20" value="#{book.price}" /></td> </tr> <tr> <td>Price :</td> <td><h:inputText size="20" value="#{book.qty}" /></td> </tr> </table> </h:form> </h:body> </html>
以下代码来自table-style.css。
.book-table-header{ bbook-bottom:1px solid #BBB; padding:16px; } .book-table-odd-row{ bbook-top:1px solid #BBB; } .book-table-even-row{ bbook-top:1px solid #BBB; }
下面的代码来自UserBean.java。
package cn..common; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import javax.faces.model.ArrayDataModel; import javax.faces.model.DataModel; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="book") @SessionScoped public class UserBean implements Serializable{ private static final long serialVersionUID = 1L; String bookNo; String productName; BigDecimal price; int qty; private static final Book[] bookList = new Book[]{ new Book("1", "CSS", new BigDecimal("123.12"), 1), new Book("2", "HTML", new BigDecimal("321.12"), 2), new Book("3", "SQL", new BigDecimal("12333.33"), 8), new Book("4", "Javascript", new BigDecimal("1233.33"), 3), new Book("5", "Web", new BigDecimal("123.22"), 10) }; public String getBookNo() { return bookNo; } public void setBookNo(String bookNo) { this.bookNo = bookNo; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } private DataModel<Book> book = new ArrayDataModel<Book>(bookList); public DataModel<Book> getBookList() { return book; } public static class Book{ String bookNo; String productName; BigDecimal price; int qty; public Book(String bookNo, String productName, BigDecimal price, int qty) { this.bookNo = bookNo; this.productName = productName; this.price = price; this.qty = qty; } public String getBookNo() { return bookNo; } public void setBookNo(String bookNo) { this.bookNo = bookNo; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } } }下载 DataTable_Row_Number.zip
将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成启动后,在浏览器地址栏中键入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml
JSF教程 -JSF ActionListener示例我们可以处理用户点击事件为h:commandButton或h:link。要注册事件处理程序,我们可以传递UI Com...
JSF教程 -JSF角色示例以下代码显示了如何在JSF应用程序中检查用户角色。例子以下代码来自demo.xhtml。?xml version=1.0 encoding...
JSF教程 - 基于JSF Bean的导航示例我们还可以在托管bean中定义一个方法来返回视图名称。下面的代码定义了一个名为NavigationCont...
Java教程 -Java异常异常是在运行时在代码序列中出现的异常状况。例如,读取一个不存在的文件。Java异常是描述异常条件的对象发生...
Java教程 -Java类变量三种类型的变量Java支持三种不同生存期的变量:成员变量方法局部变量静态变量类成员变量在创建实例时创建类...