Introduction to JavaServer Faces Part 2 - Authoring the JSP Page for the Validator Example (
Page 6 of 6 )
The JSP page is the only component in this application. It is called validatorTest.jsp and is shown in Listing 6.
Listing 6 The validatorTest.jsp Page
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>Validator Test</title>
</head>
<body>
<f:use_faces>
<h:form formName="myForm">
Enter a minimum of 6 characters here:
<h:input_text>
<f:validate_length minimum="6"/>
</h:input_text>
<br/>
<h:command_button label="Submit" commandName="submit"/>
<br/>
<h:output_errors/>
</h:form>
</f:use_faces>
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %><%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>The validatorTest.jsp page contains a form with a UIInput component represented by the input_text custom tag. The form also contains a command_button tag representing a UICommand, as well as an output_errors tag for displaying any error that occurs during processing. The most important part is the validate_length tag inside the input_text tag.
<h:input_text>
<f:validate_length minimum="6"/>
</h:input_text>

Figure 8 The directory structure for the validator example
The validate_length tag represents one of the standard validators in JSF. It checks for the length of the input and makes sure that it has a minimum of six characters. Any input shorter than six characters will generate an error that will be displayed by the output_errors tag.
Running the Validator Example
To run the application, you need to restart Tomcat. Then, use the following URL to invoke the validatorTest.jsp page:
http://localhost:8080/JSFCh02b/faces/validatorTest.jsp
Figure 9 shows the error message displayed for an input value shorter than six characters.

Figure 9 The validator example in action
Remember: This is part two of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more helpful chapters from McGraw-Hill/Osborne. Buy this book! |