background image

Adding a Custom Message

<< Registering a Validator on a Text Field | Displaying Error Messages >>
<< Registering a Validator on a Text Field | Displaying Error Messages >>

Adding a Custom Message

<h:inputText id=
"userNo" label="User Number"
value=
"#{UserNumberBean.userNumber}">
<f:validateLongRange
minimum=
"#{UserNumberBean.minimum}"
maximum=
"#{UserNumberBean.maximum}" />
</h:inputText>
</h:form>
</f:view>
For more information on the standard validators included with JavaServer Faces technology,
see
"Using the Standard Validators" on page 361
.
Adding a Custom Message
JavaServer Faces technology provides standard error messages that display on the page when
conversion or validation fails. In some cases, you might need to override the standard message.
For example, if a user were to enter a letter into the text field on greeting.jsp, he or she would
see the following error message:
User Number:
'm' must be a number between -2147483648 and 2147483647 Example: 9346
This is wrong because the field really only accepts values from 0 through 10.
To override this message, you add a converterMessage attribute on the inputText tag. This
attribute references the custom error message:
<h:inputText id=
"userNo" label="User Number"
value=
"#{UserNumberBean.userNumber}"
converterMessage="#{ErrMsg.userNoConvert}">
...
</h:inputText>
The expression that converterMessage uses references the userNoConvert key of the ErrMsg
resource bundle. The application architect needs to define the message in the resource bundle
and configure the resource bundle. See
"Configuring Error Messages" on page 297
for more
information on this.
See
"Referencing Error Messages" on page 352
for more information on referencing error
messages.
Adding a Button
The commandButton tag represents the button used to submit the data entered in the text field.
The action attribute specifies an outcome that helps the navigation mechanism decide which
page to open next.
"Defining Page Navigation" on page 296
discusses this further.
With the addition of the commandButton tag, the greeting page looks like the following:
A Simple JavaServer Faces Application
The Java EE 5 Tutorial · September 2007
294