February 28, 2021
Hot Topics:

The First Time Ever I Saw Your Face: Getting Started with JavaServer Faces

  • By David Thurmond
  • Send Email »
  • More Articles »

The first step is to create the validator tag and link it to the validator class that is responsible for performing the validation logic. The tag for your guess validator is shown below:

package com.dlt.developer.hangman;

import javax.faces.validator.Validator;
import javax.faces.webapp.ValidatorTag;
import javax.servlet.jsp.JspException;

  * GuessValidatorTag is the tag handler class for
  * GuessValidator tag,
  * <code>guess_validator</code>.

public class GuessValidatorTag extends ValidatorTag {
   public GuessValidatorTag() {
   protected Validator createValidator() throws JspException {
      GuessValidator result = null;
      result = (GuessValidator) super.createValidator();

      return result;

} // GuessValidatorTag

The validator tag is very similar to any other custom JSP tag. However, there are two things that must occur for this tag to be used as a validator. The first is the linkage between the validator tag and the validator class that will actually do the work of validation. This is done in the constructor of the GuessValidatorTag:


The string that is passed to this method is the validator ID specified in the faces-config.xml file for the GuessValidator class.

The next thing that is special about this class is that it implements the protected method createValidator(). It grabs the instance of GuessValidator that was created when the tag class was instantiated, and returns it for use by the JSF framework.

Now, you must create a JSP tag definition in a taglib configuration file to let the JSP page know about the new validator tag:

      Defines the guess-validator tag, with the tag-handler
      class, com.dlt.developer.hangman.GuessValidatorTag.
      This tag must be nested inside a UI component tag.
      The value of the UI component whose tag encloses
      the guess_validator tag is validated against the
      current puzzle.

Next, you implement the new tag on the showPage.jsp page by adding the new taglib definition to the top of the page, and changing the way you specify the validator on the guessed letter input field yet again:

<%@ taglib uri="/WEB-INF/tlds/hangman.tld" prefix="hm" %>
<h:inputText id="guessLetter" value="#{GameBean.guessedLetter}" >

Now, the GuessValidator tag can be reused anywhere that the taglib is specified, without the page author having intimate knowledge of the inner workings of the validation logic. All that is necessary is to include the taglib definition and the custom tag.


You have now built a small JSF application from the ground up; it utilizes many of the features of JSF. You have learned how to create a JSF user interface, define page navigation rules, define validation, and connect the UI to the back-end business logic of the application. Although this is a very simple example, it should get you well on your way to using this flexible, powerful framework to develop real-world JSF applications.

Download the Code

You can download the code for this article here.


About the Author

David Thurmond is a Sun Certified Developer with over fifteen years of software development experience. He has worked in the agriculture, construction equipment, financial, home improvement and logistics industries.

Page 8 of 8

This article was originally published on March 31, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date