background image

The outputLabel Tag

<< The inputText Tag | The outputLink Tag >>
<< The inputText Tag | The outputLink Tag >>

The outputLabel Tag

The required attribute causes the page to reload with errors displayed if the user does not enter
a value in the name text field. The JavaServer Faces implementation checks whether the value of
the component is null or is an empty String.
If your component must have a non-null value or a String value at least one character in
length, you should add a required attribute to your component tag and set it to true. If your
tag does have a required attribute that is set to true and the value is null or a zero-length
string, no other validators registered on the tag are called. If your tag does not have a required
attribute set to true, other validators registered on the tag are called, but those validators must
handle the possibility of a null or zero-length string.
The requiredMessage attribute references an error message from a resource bundle, which is
declared in the application configuration file. Refer to
"Registering Custom Error Messages" on
page 448
for details on how to declare and reference the resource bundle.
Rendering a Label with the outputLabel Tag
The outputLabel tag is used to attach a label to a specified input field for accessibility purposes.
The bookcashier.jsp page uses an outputLabel tag to render the label of a check box:
<h:selectBooleanCheckbox
id=
"fanClub"
rendered=
"false"
binding=
"#{cashier.specialOffer}" />
<h:outputLabel for=
"fanClub"
rendered=
"false"
binding=
"#{cashier.specialOfferText}"
>
<h:outputText id=
"fanClubLabel"
value=
"#{bundle.DukeFanClub}" />
</h:outputLabel>
...
The for attribute of the outputLabel tag maps to the id of the input field to which the label is
attached. The outputText tag nested inside the outputLabel tag represents the actual label
component. The value attribute on the outputText tag indicates the text that is displayed next
to the input field.
Instead of using an outputText tag for the text displayed as a label, you can simply use the
outputLabel
tag's value attribute. The following code snippet shows what the previous code
snippet would look like if it used the value attribute of the outputLabel tag to specify the text of
the label.
<h:selectBooleanCheckbox
id=
"fanClub"
rendered=
"false"
binding=
"#{cashier.specialOffer}" />
<h:outputLabel for=
"fanClub"
Adding UI Components to a Page Using the HTML Component Tags
Chapter 11 · Using JavaServer Faces Technology in JSP Pages
333