Page 2 of 5
JSF components also maintain a list of attributes. Those attributes store component-specific information. For example, you may want to store the URL associated with an image used by a component; so you could store that URL—or the image itself—in the component's list of attributes. Component attributes are stored by name in a hash map.
All JSF components perform three fundamental tasks:
JSF components can render themselves or delegate rendering to a renderer. The boolean UIComponent.rendersSelf() method tells the JSF implementation whether or not a component renders itself; if not, the JSF implementation obtains a reference
to the component's renderer with the UIComponent.getRendererType() method's help and then calls on the renderer to produce markup for the component.
JSF component event handling can also be managed directly by a component, or components can delegate to an event handler. One or more event handlers can be registered for a component, typically by a component's renderer or the component itself, during the JSF lifecycle's Apply Request Values phase.
Finally, JSF components can have one or more validators that validate input. Those validators, which are usually created by the JSF implementation, are stored by components in an array list.
Now that we have a basic understanding of JSF components, let's look at implementing a custom validator and associating it with a component.
In Part 1, we discussed using built-in validators to validate input for JSF components. If you use JSP for your Web application's views—which
is typically the case—you can specify validators for JSF components with a <faces:validator> tag, like this:
<faces:textentry_input id='name'> <faces:validator className='javax.faces.validator.RequiredValidator'/> </faces:textentry_input>
The code fragment attaches a validator to a text field; you just specify the validator's class name with the <faces:validate> tag's className attribute. The validator specified above is a JSF built-in validator that checks to make sure a component's value is not
null. As discussed in Part 1, JSF provides a handful of built-in validators, but you can also implement your own validators and
associate them with a JSF component. For example, the application shown in Figure 3 uses a custom validator to validate a
username.
Figure 3. Use a custom validator. Click on thumbnail to view full-size image.
The JSP page displayed in Figure 3 is listed in Listing 1.
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
<html>
<head>
<title>A Simple JavaServer Faces Application</title>
</head>
<body>
<%@ taglib uri='http://java.sun.com/j2ee/html_basic/' prefix='faces' %>
<font size='4'>Please enter your name and password</font>
<faces:usefaces>
<faces:form id='simpleForm' formName='simpleForm'>
<table>
<tr>
<td>Name:</td>
<td>
<faces:textentry_input id='name'>
<faces:validator
className='com.sabreware.validators.NameValidator'/>
<faces:validator
className='javax.faces.validator.LengthValidator'/>
<faces:attribute
name='javax.faces.validator.LengthValidator.MINIMUM'
value='3'/>
</faces:textentry_input>
</td>
<td>
<faces:validation_message componentId='name'/>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<faces:textentry_secret id='password'/>
</td>
</tr>
</table>
<p><faces:command_button id='submit' commandName='Log In'/>
</faces:form>
</faces:usefaces>
</body>
</html>
Like the code fragment shown at the beginning of this section, the preceding JSP page uses <faces:validator> tags to attach validators to a JSF component. In this case, the validators are a custom validator that authenticates a username
and a JSF built-in validator that checks to make sure the username is at least three characters long.