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

JBoss Tools 3 Developer Guide

  • June 3, 2009
  • By Anghel Leonard
  • Send Email »
  • More Articles »

Working with Custom Converters

Creating and registering a custom converter is a very elegant method for accomplishing conversion issues that can't be resolved by JSF default converters. For example, in your registration form, you need the following conversions:
  • The username (personName bean property) doesn't need a conversion as the name is a String.
  • The user age (personAge bean property) needs to be converted from a String to an Integer. you will use a default JSF converter.
  • The user birth date (personBirthDate bean property) needs to be converted from a String to a java.util.Date instance. This conversion can be accomplished by the JSF f:convertDateTime default converter.
  • The user phone number (personPhone bean property) needs to be converted from a String to a register.PhoneNumber instance. This task can be accomplished only by a custom converter, because no default converter can help you obtain this conversion.

Now, creating a custom converter can be done like this:

  1. Click on the Converters node inside the left panel of Tree view.

  2. From the right panel of Tree view, click on the Add button for opening the Add Converter window (as shown in Figure 23).



    Click here for larger image

    Figure 23: The Add Converter Window

  3. Fill in the Converter-ID field with the converter name and the Converter-Class field with the converter class name. If you want to customize the converter class, click on the Converter-Class link (in this example, you will use the default settings).

  4. In the Add Converter window, click on the Finish button for generating the new converter.

Edit the converter-generated source code by clicking on the Converter-Class link that appears in the right panel of the Tree view (as shown in Figure 24).



Click here for larger image

Figure 24: Edit the Converter-Generated Source Code

Note that you have to manually insert the behavior of the getAsObject and getAsString methods. For example, you convert the inserted phone number into a register.PhoneNumber object by adding the following code to these methods:

import java.util.StringTokenizer;
...
  public Object getAsObject(FacesContext arg0, UIComponent arg1,
String arg2) {
    // TODO Auto-generated method stub
    System.out.println("//getAsObject method//");
    StringTokenizer st = new StringTokenizer(arg2,"-");
    PhoneNumber pn = new PhoneNumber();
    try{
        pn.setAllNumber(arg2);
        pn.setCountryCode(st.nextToken());
        pn.setAreaCode(st.nextToken());
        pn.setPrefixNumber(st.nextToken());
        }catch (Exception e){ return pn; }
    return pn;
   }
   public String getAsString(FacesContext arg0, UIComponent arg1, 
Object arg2) {
    // TODO Auto-generated method stub
    System.out.println("//getAsString method//");
    if(arg2 != null)
      {
       String value = ((PhoneNumber)arg2).getAllNumber();
       return value;
      }
   return "";
}
Finally, save the project state by selecting the Save option from the File main menu.

Working with Custom Validators

When JSF default validators don't satisfy your application's needs, it is time to implement a custom validator. For example, for your registration form, you have the following situation:
  • The username (personName bean property) doesn't need validation.
  • The user age (personAge bean property) needs to be a valid age. This can be accomplished with the JSF f:validateLongRange default validator.
  • The user birth date (personBirthDate bean property) doesn't need validation.
  • The user phone number (personPhone bean property) needs to be validated as a string that respects the next pattern: x[x]-xxx-xxx-xxxx. This task can be accomplished only by a custom validator because no default validator can help us.

Now, creating a custom validator can be done like this:

  1. Click on the Validators node inside the left panel of Tree view.

  2. From the right panel of Tree view, click on the Add button for opening the Add Validator window (see Figure 25).



    Click here for larger image

    Figure 25: The Add Validator Window

  3. Fill in the Validator-ID field with the validator name and the Validator-Class with the validator class name. If you want to customize the validator class, click on the Validator-Class link. (In this example, you will use the default settings.)

  4. In the Add Validator window, click on the Finish button for generating the new validator.

Edit the validator-generated source code by clicking on the Validator-Class link that appears in the right panel of the Tree view as shown in Figure 26.



Click here for larger image

Figure 26: Edit the Validator-Generated Source Code

Notice that you have to manually insert the behavior of the validate method. For example, you validate the inserted phone number by adding the following code to this method:

import javax.faces.application.FacesMessage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
      throws ValidatorException {
    // TODO Auto-generated method stub
    System.out.println("//validate method//");
      String value = ((PhoneNumber)arg2).getAllNumber();
      String countryCode = "^[0-9]{1,2}";
      String areaCode = "( |-|(){1}[0-9]{3}( |-|)){1}";
      String prefixNumber = "( |-)?[0-9]{3}";
      String number = "( |-)[0-9]{4}$";
      Pattern mask = Pattern.compile(countryCode + areaCode +
                     prefixNumber + number);
       Matcher matcher = mask.matcher(value);
      if (!matcher.find()){
        FacesMessage message = new FacesMessage();
        message.setDetail("Your phone number is not valid!");
        message.setSummary("Your phone number is not valid!");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
     }
}
Finally, save the project state by selecting the Save option from the File main menu.

In addition to this, JSF Tools allows you to create custom referenced beans (note that referenced beans are used very rarely and they basically are Java Beans available in a JSF scope—the application must create an instance of the referenced bean and place it in the desired scope). For this, select the Referenced Beans node (Tree view) and click the Add button from the right panel. In the Add Referenced Bean wizard, fill up the Referenced-Bean-Name with a name for your referenced bean and the Referenced-Bean-Class with the full name of the referenced bean class. Optionally, click on the Referenced-Bean-Class link if you want to customize the referenced bean stub class. Click Finish.

The Source View

In the Source view, you can see the source of the faces-config.xml document. This source is synchronized with the other two views, so you will always see the reflection of your modifications. This view allows you to modify the configuration file manually or by using the Outline view that displays a tree view of it (see Figure 27).



Click here for larger image

Figure 27: Using the Outline View

Summary

In this article, you have seen how to use JSF Tools for modeling the most important components of a traditional JSF project. You have developed a complete functional JSF project by using the most important skills of JSF Tools like managed beans, converters, validators.

About the Book Excerpt

This article is an excerpt from the book, JBoss Tools 3 Developers Guide, by Anghel Leonard, published by Packt Publishing. The book teaches Java developers how to develop JSF, Struts, Seam, Hibernate, jBPM, ESB, web services, and portal applications rapidly using JBoss Tools for Eclipse and the JBoss Application Server.

About the Author

Anghel Leonard is a senior Java developer with more than 12 years of experience in Java SE, Java EE, and the related frameworks. He has written and published dozens of articles about Java technologies and two books about XML and Java (one for beginners and one for experts).



Page 5 of 5



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel