New Data Binding features in Grails 2.3

23 / Mar / 2014 by Imran Mir 3 comments

We recently upgraded to Grails 2.3 and found some pleasant features in the rewritten Data Binding. This raised my interest to look further into what is new and found some interesting things that could be useful:

  1. We can create a global setting for the dataBinding of the dates using the following setting in Config:[java]grails.databinding.dateFormats = [‘MMddyyyy’, ‘yyyy-MM-dd HH:mm:ss.S’, "yyyy-MM-dd’T’hh:mm:ss’Z’"][/java]

    The formats specified will be attempted in the order in which they are included in the List. The second and third format in the list above
    are the default ones.

  2. It is possible that you may want to override these global settings. This can be done using the @BindingFormat(‘MMddyyyy’) annotation as follows:[java]

    import org.grails.databinding.BindingFormat
    class Person {
    @BindingFormat(‘MMddyyyy’)
    Date birthDate
    }[/java]

    BindingFormat also supports localization, with the help of optional code attribute:

    [java]

    class Person {
    @BindingFormat(code=’date.formats.birthdays’) // date.formats.birthdays are defined in message.properties
    Date birthDate
    }

    [/java]

  3.  You can also write your own handler for the bindingFormat, by implementing org.grails.databinding.converters.FormattedValueConverter into a custom class and register the bean in Spring Application Context.
  4. There is also support for the Structured Data Binding Editors which help binding structured params to a property, i.e., we can integrate serveral pieces of data in
    params into a single value. The common example can be to create a date value from day, month and year as separate params. All you need to do is to
    extend the org.grails.databinding.converters.AbstractStructuredBindingEditor, override the getPropertyValue and register the instance with
    Spring Application Context.
  5. There is also support for the Data Binding Event Listeners. The simplest way to do this would be to extend org.grails.databinding.events.DataBindingListenerAdapter
  6. And the last but not the least: How many times have you felt the need to access a bindData mehtod in a service (or for that matter have your own implementation of the bindData) . Now you can do it simply by injecting “grailsWebDataBinder” in the service as:[java]

    class NeedsBindDataService {
    def grailsWebDataBinder
    def needsBinding(Widget widget, Map data) {
    grailsWebDataBinder.bind widget, data as SimpleMapDataBindingSource
    }
    }[/java]

    All this is well documented here.

Apart from these, there are some other improvements and bug fixes. I found the following worth mentioning as they were related to the project I work on:

  1.  Earlier, to bind collections into the command objects was not that straight forward. We used to do something like this in command objects: [java]class SchoolCO{
    List<StudentCommand> students = ListUtils.lazyList([], {new StudentCommand()} as Factory)
    }[/java]

    But, now with new dataBinding, all you need to do is to declare students:[java]

    class StudentCO{
    List<StudentCommand> students
    }[/java]

  2. There also was a bug due to which bindable: false for collections was being ignored. Now, this has also been fixed.
Hope you all find it useful.

Cheers,

Imran Mir

 

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. Ajay Singh

    can we use @bindingFormat in parent child relationship where parent class is an abstact class
    like this ” abstract class BasePrescriptionRx extends ClientAuditable implements Serializable {}”
    I want to use @bindingFormat in this

    Reply
  2. RMorrisey

    It took me a long time to figure out, so I thought I would share this…
    For ISO 8601, I used:
    grails.databinding.dateFormats = [“yyyy-MM-dd’T’HH:mm:ss.SSSX”, “yyyy-MM-dd’T’hh:mm:ssX”]
    I don’t think this covers the entire ISO spec but it was good enough for my use case.
    If you run into trouble with date formatting issues using this setting, put a breakpoint in the DateConversionHelper#convert() method in the grails framework code.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *