Java Reference
In-Depth Information
</f:facet>
<h:outputText value="#{item.age}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
</body>
</html>
In this example, we will be iterating through a collection of
RegistrationBean
objects. The objects will be stored as a property named
registrationBeanList
of
type
java.util.List
in a managed bean called
RegistrationListController
,
therefore we set the value of the
value
attribute of
<h:dataTable>
to
#{RegistrationListController.registrationBeanList}
.
NetBeans creates a sensible value for the
var
attribute, therefore we leave it as is.
<h:dataTable>
contains a few attributes that allow us to control the look of the
generated table. These attributes are identical to attributes in a standard HTML table.
In our example, we set a border of 1 pixel in the table by setting the value of the
border
attribute to
1
. We set the spacing between table cells to zero by setting the
cellspacing
attribute to
0
. We also set the spacing (padding) inside table cells to 5
pixels by setting the
cellpadding
attribute to
5.
The complete list of attributes for
<h:dataTable>
can be seen by using
code completion (
Ctrl+Space
).
Since our table will have four columns, we need to add four nested
<h:column>
tags
into our data table (one for each column).
Notice each
<h:column>
tag has a nested
<f:facet>
tag. JSF tags might define
one or more facets. Facets are components that are rendered differently from other
components in the parent component. Each facet must have a unique name for each
parent component.
<h:column>
defines a facet with a name of
header
, this facet
will be rendered as the header of the generated table. To render a facet inside a JSF
component, the
<f:facet>
tag is used. In our example we give our facet the name
of
header
by assigning this value to its
name
property. At runtime, JSF renders the
tag inside
<f:facet>
as the header of the column rendered by the facet's parent
<h:column>
tag. Each
<f:facet>
tag must have a single child tag, which can be any
HTML JSF tag.