Announcement Announcement Module
No announcement yet.
Binding Numeric ID Field Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding Numeric ID Field

    I'm getting a bind error on my SimpleFormController. It's occuring on my property "id" which is a Long in my domain object. I'm getting this error upon submitting the form:

    Failed to convert property value of type [java.lang.String] to required type [java.lang.Long] for property 'id'; nested exception is java.lang.NumberFormatException: For input string: ""

    The error makes sense to me... it cannot bind to the "id" field because it can't convert "" to a Number using the NumberFormat.

    My desired effect here is to have the binder set id = null (or not set it at all). I would presume this would be the default behavior but I guess I'm wrong. Do I need to register a Custom Property Editor for every one of my id fields?

    I can't seem to find an example of a similar problem. Thanks for your time.

  • #2
    Re: Binding Numeric ID Field

    Yeah, you would have to make a custom editor that checked for empty string values and converted them to null values in this case. There might be a property on the Long custom property editor in Spring, but I haven't looked at it in a long time. I got annoyed with this behavior too so I just added a feature into my framework that had a setting to allow null values or not, so it will throw the exception only when you want it to, but this setting is usable for longs, dates, booleans, user-defined types or whatever instead of just certain editors.


    • #3
      Thanks for the quick reply. I looked through the Spring API Javadocs and found the CustomNumberEditor class.

      Here's how I'm using it:

      binder.registerCustomEditor(DomainObject.class,"id ",new CustomNumberEditor(Long.class,true));

      I've moved this code into an AbstractMyAppFormController class so that the "custom" binding of the empty String for null values is all in one place for the whole app.