Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails data binding

I'm trying to use DataBindingUtils.bindObjectToInstance(object, source) to copy properties from one object to another using this code (which can be run in the Grails console):

import org.codehaus.groovy.grails.web.binding.DataBindingUtils

class Source {

  String foo = 'foo'
  String bar = 'bar'
}

class Target {

  String foo
  String bar
}

def s = new Source()
def t = new Target()

def result = DataBindingUtils.bindObjectToInstance(t, s)

assert t.foo == 'foo'
assert t.bar == 'bar'

But the assertions are failing because the properties of t are null, why?

like image 470
Dónal Avatar asked May 20 '26 14:05

Dónal


1 Answers

The data binder (below) in Grails has only implemented binding for a select subset of object types, namely Maps and Web Requests. So binding two arbitrary objects isn't implemented.

https://github.com/grails/grails-core/blob/master/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/binding/DataBindingUtils.java

like image 75
schmolly159 Avatar answered May 22 '26 11:05

schmolly159



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!