Java Reference
In-Depth Information
When using
<h:label>
tags to generate labels for input fields, or when using
<h:message>
tags to generate validation errors, we need to explicitly set the value
of the
id
tag, since we need to specify it as the value of the
for
attribute of the
corresponding
<h:label>
and
<h:message>
tags.
Every JSF input tag has a
label
attribute. This attribute is used to generate
validation error messages on the rendered page. If we don't specify a value for the
label
attribute, then the field will be identified in the error message by it's ID.
Each JSF input field has a
value
attribute. In the case of
<h:selectOneMenu>
, this
attribute indicates which of the options in the rendered
<select>
tag will be selected.
The value of this attribute must match the value of the
itemValue
attribute of one of
the nested
<f:selectItem>
tags. The value of this attribute is usually a
value binding
expression
, which means that the value is read at runtime from a JSF-managed bean.
In our example, the value binding expression
#{RegistrationBean.salutation}
is used. What will happen is, at runtime JSF will look for a managed bean named
RegistrationBean
, and look for an attribute named
salutation
on this bean, the
getter
method for this attribute will be invoked, and its return value will be used to
determine the selected value of the rendered HTML
<select>
tag.
Nested inside the
<h:selectOneMenu>
there are a number of
<f:selectItem>
tags.
These tags generate HTML
<option>
tags inside the HTML
<select>
tag generated
by
<h:selectOneMenu>
. The value of the
itemLabel
attribute is the value that the
user will see, while the value of the
itemValue
attribute will be the value that will be
sent to the server when the form is submitted.
All other rows in our grid contain
<h:inputText>
tags. This tag generates an HTML
input
field of type
text
, which accepts a single line of typed text as input. We
explicitly set the
id
attribute of all of our
<h:inputText>
fields; this allows us to
refer to them from the corresponding
<h:outputLabel>
and
<h:message>
fields.
We also set the
label
attribute for all of our
<h:inputText>
tags; this results in user
friendlier error messages.
Some of our
<h:inputText>
fields require a value. These fields have their
required
attribute set to
true
, and each JSF input field has a
required
attribute. If we require
the user to enter a value for this attribute, then we need to set this attribute to
true
.
This attribute is optional, and if we don't explicitly set a value for it, then it defaults
to false.