Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using Convert to handle NULL values for empty Strings ""

843844Oct 19 2008 — edited Oct 20 2008
After having had the problem with null values not being returned as nulls and reading some suggestion solution I added a converter to my application.
...
  <converter>
    <converter-id>NullStringConverter</converter-id>
    <converter-for-class>java.lang.String</converter-for-class>
    <converter-class>com.j2anywhere.addressbookserver.web.NullStringConverter</converter-class>
  </converter>
...
I then implemented it as follows:

...
  public String getAsString(FacesContext context, UIComponent component, Object object)
  {
    System.out.println("Converting to String : "+object);
   
    if (object == null)
    {
      System.out.println("READING null");
      return "NULL";
    }
    else
    {
      if (((String)object).equals(""))
      {
        System.out.println("READING null (Second Check)");
        return null;        
      }
      else
      {
        return object.toString();
      }
    }   
  }
  
  public Object getAsObject(FacesContext context, UIComponent component, String value)
  {
    System.out.println("Converting to Object: "+value+"-"+value.trim().length());
    if (value.trim().length()==0 || value.equals("NULL"))
    {
      System.out.println("WRITING null");
      return null;
    }
    else
    {
      return value.toUpperCase();
    }
  }
...
I can see that it is converting my values, however the object to which the inputText fields are bound are still set to empty strings ""
<h:inputText size="50" value="#{addressBookController.contactDetails.information}" converter="NullStringConverter"/>
Also when reading the object values any nulls are already converted to empty strings before ariving at the converter. It seems that there is a default converter handling string values.

How can I resolve this problem as set nulls when the input value is an empty string other then checking every string in my class individually. I would really hate to pollute my object model with empty string tests.

Thanks in advance

Edited by: j2anywhere.com on Oct 19, 2008 9:06 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2008
Added on Oct 19 2008
11 comments
733 views