Grails binddata in service

后端 未结 3 1752
清歌不尽
清歌不尽 2021-02-04 20:20

Is there a way to utilize bindData in a service other than using the deprecated BindDynamicMethod? I can\'t just use

TestObject testOb         


        
3条回答
  •  再見小時候
    2021-02-04 20:47

    If you are using Grails 3.* then the service class can implement DataBinder trait and implement bindData() as shown below example:

    import grails.web.databinding.DataBinder
    
    class SampleService implements DataBinder {
    
        def serviceMethod(params) {
            Test test = new Test()
            bindData(test, params)
    
            test
        }
    
        class Test {
            String name
            Integer age
        }
    }
    

    This is how I quickly tried that in grails console:

    grailsApplication.mainContext.getBean('sampleService').serviceMethod(name: 'abc', age: 10)
    

提交回复
热议问题