background image

Registering a Validator on a Text Field

<< Adding an Image | Adding a Custom Message >>
<< Adding an Image | Adding a Custom Message >>

Registering a Validator on a Text Field

<%@ taglib uri=
"http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri=
"http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<h:form id=
"helloForm1">
<h2>Hi. My name is Duke. I
'm thinking of a number from
<h:outputText lang=
"en_US"
value=
"#{UserNumberBean.minimum}"/> to
<h:outputText value=
"#{UserNumberBean.maximum}"/>.
Can you guess it?</h2>
<h:graphicImage id=
"waveImg" url="/wave.med.gif" />
<h:inputText id=
"userNo" label="User Number"
value=
"#{UserNumberBean.userNumber}">
...
</h:inputText>
</h:form>
</f:view>
See
"Backing Beans" on page 309
for more information on creating beans, binding to bean
properties, referencing bean methods, and configuring beans.
See
"Using Text Components" on page 330
for more information on the inputText tag.
Registering a Validator on a Text Field
By nesting the validateLongRange tag within a text field's component's tag, the page author
registers a LongRangeValidator onto the text field. This validator checks whether the
component's local data is within a certain range, defined by the validateLongRange tag's
minimum
and maximum attributes.
In the case of the greeting page, you need to validate the number the user enters into the text
field. So, you add a validateLongRange tag inside the inputText tag. The maximum and minimum
attributes of the validateLongRange tag get their values from the minimum and maximum
properties of UserNumberBean using the value expressions #{UserNumberBean.minimum} and
#{UserNumberBean.maximum}
. See
"Backing Beans" on page 309
for details on value
expressions.
After adding the validateLongRange tag, the page looks like this:
<%@ taglib uri=
"http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri=
"http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<h:form id=
"helloForm1">
<h2>Hi. My name is Duke. I
'm thinking of a number from
<h:outputText lang=
"en_US"
value=
"#{UserNumberBean.minimum}"/> to
<h:outputText value=
"#{UserNumberBean.maximum}"/>.
Can you guess it?</h2>
<h:graphicImage id=
"waveImg" url="/wave.med.gif" />
A Simple JavaServer Faces Application
Chapter 10 · JavaServer Faces Technology
293