The First Time Ever I Saw Your Face: Getting Started with JavaServer Faces
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() {
super();
super.setValidatorId("guessValidator");
}
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:
super.setValidatorId("guessValidator");
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:
<tag>
<name>guess_validator</name>
<tag-class>
com.dlt.developer.hangman.GuessValidatorTag
</tag-class>
<description>
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.
</description>
</tag>
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}" >
<hm:guess_validator/>
</h:inputText>
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.
Conclusion
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.
References
- Java Community Process JSF Site: http://www.jcp.org/en/jsr/detail?id=127
- JavaServerFaces API Documentation: http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/api/index.html
- JavaServer Faces Tutorial: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSFIntro.html
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.



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.