September 18, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

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

  • March 31, 2008
  • By David Thurmond
  • Send Email »
  • More Articles »

In this case, you'll implement the same logic that you did in the GameBean's validate() method, but the validator will grab all of the necessary data from the JSF framework, rather than from its own internal instance variables. The code for this validator is shown below:

package com.dlt.developer.hangman;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
import javax.faces.validator.Validator;
import javax.faces.component.UIComponent;
public class GuessValidator implements Validator {

   public void validate(FacesContext facesContext,
      UIComponent uIComponent, Object value)
      throws ValidatorException {

      GameBean theGameBean = (GameBean)
         facesContext.getApplication().getVariableResolver().
         resolveVariable(facesContext, "GameBean");

      String msg1 = theGameBean.getBundle().
         getString(GameBean.GUESS_A_LETTER_MSG);
      String msg2 = theGameBean.getBundle().
         getString(GameBean.LETTER_ALREADY_GUESSED_MSG);

      // Get the component's contents and cast it to a String
      String theLetter = value.toString().trim().toUpperCase();

      if (theLetter.length() == 0) {
         FacesMessage message = new FacesMessage();
         message.setDetail(msg1);
         message.setSummary(msg1);
         message.setSeverity(FacesMessage.SEVERITY_ERROR);
         throw new ValidatorException(message);
      ) // if

      String alreadyGuessedLetters =
         theGameBean.getPuzzleInstance().getGuessedLetters();
      if (alreadyGuessedLetters.contains(theLetter)) {
         FacesMessage message = new FacesMessage();
         message.setDetail(msg2);
         message.setSummary(msg2);
         message.setSeverity(FacesMessage.SEVERITY_ERROR);
         throw new ValidatorException(message);
      } // if

   } // validate()

// GuessValidator

The first thing to note about the GuessValidator class is that it implements the Validator interface. This interface must be implemented for any class that performs validation in JSF. The only method that this interface defines is the validate() method, as shown.

The only other bit of razzle-dazzle in this class is the fact that you are grabbing an instance of the GameBean class to check whether the letter the user entered was already in the puzzle:

GameBean theGameBean =
   (GameBean) facesContext.getApplication().getVariableResolver().
   resolveVariable(facesContext, "GameBean");

Here, you grab an instance of the JSF application object, get a variable resolver, and ask it to get the current instance of the GameBean, so that you can have access to the current puzzle value and already guessed letters.

As before, if any of the validation checks fails, you throw a ValidationException, and the message is displayed wherever the <h:message> tag for the UI component appears.

Now, you must tell JSF that you want to use this validator class for the guessed letter input field:

<h:inputText id="guessLetter"
             value="#{GameBean.guessedLetter}" >
<f:validator validatorId="guessValidator"/>
</h:inputText>

There is one last detail, however, that you must take care of to get this validator working. A validator entry needs to be placed in the faces-config.xml file so that the validatorId attribute on your <f:validator> tag can be resolved. The validator tag is shown below:

<validator>
   <description>
      Registers the guess validator class
   </description>
   <validator-id>guessValidator</validator-id>
   <validator-class>
      com.dlt.developer.hangman.GuessValidator
   </validator-class>
</validator>

Note that the validatorId attribute on the <f:validator> tag matches the <validator-id> tag's value above. Now, you can reuse this validator anywhere in the application by simply specifying a <f:validator> tag with the validatorId set as you have done here.

Creating a Validator Tag

Although creating a validator class does add a great deal of reusability to the validation logic that is written, there may be cases where even more complexity is desired for validation. Even though this is not the case with your guess validation scenario, consider the case where the JSP page author needs to specify some values that must be used at validation time to perform the check, such as a list of acceptable phone number formats. Under these circumstances, it is best to wrap the validator class in a special validator tag for maximum flexibility and reusability.





Page 7 of 8



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel