开发者

Using bindData method outside of controller

I was wondering if anyone had an idea for the best way to provide the functionality of bindData() outside of my grails controllers. In my current project I have created several groovy classes to model objects returned by an api. In these classes I have a static method that parses xml and returns a List of objects of the class. I would like to 开发者_开发知识库skip all the type casting nonsense by using the bindData method in these classes. Any suggestions on how to do this would be appreciated.


I was looking for a similar solution, to use bindData in a service class. I found a solution in JT's blog. The solution is basically to import:

import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod

then add this to your code:

def foo = new Foo()
BindDynamicMethod bind = new BindDynamicMethod()
def args =  [ foo, params, [exclude:['name', 'mail']] ] // for example
bind.invoke( foo, 'bind', (Object[])args)

The (Object[]) cast is necessary du to Groovy/Java compatability. (Groovy is treating the ‘args’ object as an ArrayList, not an array of Objects.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜