O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Validation

Most software requires some kind of data entry, and in order to be user-friendly, an application should support the user as much as possible throughout the data entry process. This means identifying invalid input and giving the user a chance to fix it sooner rather than later.

JSF, as you might expect, has built-in support for validation . All JSF user interface components support validation through both built-in and custom validation rules.

Let’s take a look at the Add Books form (Figure 5-4) in the JSF Library, which needs to enforce minimum field lengths and mandatory fields. The Add Books function is available to librarians who have logged into the system. It allows them to fill out a simple form to create a new book.

Adding Validators to Forms

Example 5-8 shows the JSP source for the Add Books form. We’re using three different kinds of validation: required fields, built-in validators, and a custom validator (which we’ll see in more detail in the next section). We’re also using the <h:message> tag to display validation messages for each component.

Add Books form

Figure 5-4. Add Books form

Example 5-8. /view/addbook.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <html> <f:view> <head> <title>Add Book to Library</title> <link rel="stylesheet" type="text/css" href='<%= request.getContextPath() + "/css/app.css" %>'> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required