This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSF and the fly likes Return null from a Converter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Return null from a Converter?" Watch "Return null from a Converter?" New topic
Author

Return null from a Converter?

Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
I've written a custom converter but I'm having a problem setting a null/empty value with it. If a user inputs a value, such as "123abc", into an inputText and the value is applied through a submit or an ajax call and then later that value is deleted from the inputText by the user, the old value, "123abc", remains upon the next submit or ajax call. The behavior I want is for that property bound to the value of the inputText to be set to null. In my converter in getAsObject() I am returning null if the incoming String has a length of zero and that seems to be the issue. But since the property classtype may be java.util.Date I don't want to try and assign an empty String to it. Any ideas?
Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
It seems that returning null from getAsObject() causes no decoding to be done on the request value. I have looked but can't find that behavior documented anywhere. Can anybody confirm this?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

I can't point you to any specific documentation, but yes, JSF, like HTML, is quite averse to NULL data. And I can tell you from painful experience that NULL values in databases can be only slightly less injurious to one's sanity than attempting to use dates as database keys.

I prefer Calendar date controls like many extension tagsets offer anyway. If I had a date item and really truly wanted it to be nullable, I'd generally use a fa├žade property on the backing bean and use that to populate the nullable persistent property that it corresponded to. I would then handle the null values as part of the general store/fetch code for that property.


Customer surveys are for companies who didn't pay proper attention to begin with.
Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
I've been thinking about creating all my properties as Strings on the backend. One of the reasons is it makes it easier to get some AJAX working because my converters are throwing exceptions. But what kind of problems or headaches would I have if I did this? I would have to move my validation to a separate method and call it somehow upon submission. Am I asking for trouble with that approach?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return null from a Converter?