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.

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" %>'> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access