JSF input conversion - empty string becomes zero instead of null

Discussions

Web tier: servlets, JSP, Web frameworks: JSF input conversion - empty string becomes zero instead of null

  1. Hi, I have an input box that takes a number - a simple int. This box corresponds to a db column that accepts null values. I'd like to send null to DAO layer when user enters nothing (empty string). The problem is that JSF automatically converts empty string to zero (int). I tried to provide my own converter, and I can convert the empty string into any value (say, -1 for example) but still not null! When my converter returns null DAO receives again zero. I tried one more thing - marking the field as required and providing my own validator (that would ignore empty string): But here I ran into yet another problem: for some reason my validator is ignored and I always get a message "field required blah blah ...". Does anyone know how to send that null to the db? I'm not sure what to try next ... Thanks!
  2. I think you have required="true" so there is no way to leave the field as blank. Integer object can be used to capture null integer value. - http://venkatsadasivam.wordpress.com/
  3. I only added the required="true" attribute to the input box to force JSF to call my own validator (which I thought would let me pass null; and JSF does not call that validator either way for some reason). The original problem is that JSF is changing empty string into zero Integer. How to make it change empty string into null Integer? When I provide my own converter I can convert empty string into any value (say, -1), and that value gets propagated to the DAO layer no problem. But as soon as I return null the value I get in the DAO layer is zero. I'm not sure why is JSF making that arbitrary decision but the bottom line is I need to change it to null.
  4. Don't use primitive type "int". Use wrapper object Integer, which help you to represent empty string as NULL value. The same problem already discussed in sun forum as well. http://forums.sun.com/thread.jspa?threadID=5255449&messageID=10058766
  5. hi Mark, if your application is running under tomcat, setting org.apache.el.parser.COERCE_TO_ZERO system property to false might help you. Coercing value expressions runs after validations and conversions that is why you always got zero instead of null. You can change the default behavior by passing the following argument to tomcat startup script: -Dorg.apache.el.parser.COERCE_TO_ZERO=false "If true, when coercing expressions to numbers "" and null will be coerced to zero as required by the specification. If not specified, the default value of true will be used." regadring to this documentation: http://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html Regards, -peter
  6. A better solution would be https://issues.apache.org/bugzilla/show_bug.cgi?id=48813