background image

Displaying Error Messages

<< Adding a Custom Message | Defining Page Navigations >>
<< Adding a Custom Message | Defining Page Navigations >>

Displaying Error Messages

<%@ 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}">
<f:validateLongRange
minimum=
"#{UserNumberBean.minimum}"
maximum=
"#{UserNumberBean.maximum}" />
</h:inputText>
<h:commandButton id=
"submit"
action=
"success" value="Submit" />
</h:form>
</f:view>
See
"Using Command Components for Performing Actions and Navigation" on page 335
for
more information on the commandButton tag.
Displaying Error Messages
A message tag is used to display error messages on a page when data conversion or validation
fails after the user submits the form. The message tag in greeting.jsp displays an error
message if the data entered in the field does not comply with the rules specified by the
LongRangeValidator
implementation, whose tag is registered on the text field component.
The error message displays wherever you place the message tag on the page. The message tag's
style
attribute allows you to specify the formatting style for the message text. Its for attribute
refers to the component whose value failed validation, in this case the userNo component
represented by the inputText tag in the greeting.jsp page.
Put the message tag near the end of the page:
<%@ 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
295