• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion issue

 
Rob Micah
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Object property in my bean that holds a Date object. I have taken great care to make sure it is never set to a String. I have even added debug print to my accessor to ensure it is returning null rather than an empty string. However in my converter getAsString() method the Object value coming in is sometimes an empty String. There must be something I am missing in the JSF lifecycle because I don't understand how this is possible. Why is this?
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF's default display mechanism is HTML. HTML is a text protocol. NULL is a binary concept. Converters are specifiically intended to bridge the gap between internal binary formats and external formats, so in the case of HTML converters - whether user-defined or built-in, the converter must return a text value. In the case of NULL objects, this usually means converting NULL into an empty String.

Going the opposite direction is even harder, since there's nothing in HTML itself that can tell a converter whether an incoming empty string should result in an actual empty String object or a NULL object.

To minimize pain, it's better to use empty strings instead of NULLs to represent missing text values in a backing bean. I do have one exception to this rule, which is when I want a "get" method to construct and return an object that needs extensive (re)construction, but those objects are usually things like SelectItem collections, not simple objects like Dates.
 
Rob Micah
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I completely understand what you're saying. But what's confusing me is where the value argument is coming from in the getAsString method of the converter interface

I was under the impression that the value argument was what was returned from the property's get method inside its bean. If so then how can it ever be an empty String when I never return an empty String from that method.
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll wager that if you put a trace on the corresponding "set" method you'll discover some things.

Although the BeanUtils may be having their say, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!