ui:repeat
始终用作h:dataTable
的替代方法,以遍历数组或列表以HTML表格格式显示数据。 请参阅以下示例:
1. h:dataTable
在dataTable中,JSF帮助您生成所有HTML表标记。
<h:dataTable value="#{order.orderList}" var="o">
<h:column>
#{o.orderNo}
</h:column>
<h:column>
#{o.productName}
</h:column>
<h:column>
#{o.price}
</h:column>
<h:column>
#{o.qty}
</h:column>
</h:dataTable>
2. ui:repeat
在重复标记中,您必须手动放置所有HTML表标记。
<table>
<ui:repeat var="o" value="#{order.orderList}" varStatus="status">
<tr>
<td>#{o.orderNo}</td>
<td>#{o.productName}</td>
<td>#{o.price}</td>
<td>#{o.qty}</td>
</tr>
</ui:repeat>
</table>
ui:repeat示例
这是一个JSF 2.0 ui:repeat示例,用于呈现完全相同的HTML输出,例如h:dataTable示例 。 比较两者并找出不同之处。
JSF…
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<h1>JSF 2 ui:repeat tag example</h1>
<table class="order-table">
<tr>
<th class="order-table-header">Order No</th>
<th class="order-table-header">Product Name</th>
<th class="order-table-header">Price</th>
<th class="order-table-header">Quantity</th>
</tr>
<tbody>
<ui:repeat var="o" value="#{order.orderList}" varStatus="status">
<h:panelGroup rendered="#{status.even}">
<tr>
<td class="order-table-even-row">#{o.orderNo}</td>
<td class="order-table-even-row">#{o.productName}</td>
<td class="order-table-even-row">#{o.price}</td>
<td class="order-table-even-row">#{o.qty}</td>
</tr>
</h:panelGroup>
<h:panelGroup rendered="#{status.odd}">
<tr>
<td class="order-table-odd-row">#{o.orderNo}</td>
<td class="order-table-odd-row">#{o.productName}</td>
<td class="order-table-odd-row">#{o.price}</td>
<td class="order-table-odd-row">#{o.qty}</td>
</tr>
</h:panelGroup>
</ui:repeat>
</tbody>
</table>
</h:body>
</html>
注意
您可以在此h:dataTable示例中找到“订单”托管Bean源代码。
“ ui:repeat ”标记带有许多有用的属性,例如offset , size , status等。请确保选中此JSF ui:repeat javadoc 。
输出量
下载源代码
下载它– JSF-2-Repeat-Tag-Example.zip (10KB)
参考文献
标签: jsf2