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! |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |