Grails binddata in service

后端 未结 3 1759
清歌不尽
清歌不尽 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:49

    In 2.5, I found that emulating the behaviour of the Controller API in a helper service worked:

    def bindData(def domainClass, def bindingSource, String filter) {
        return bindData(domainClass, bindingSource, Collections.EMPTY_MAP, filter)
    }
    def bindData(def domainClass, def bindingSource, Map includeExclude, String filter) {
        DataBindingUtils
            .bindObjectToInstance(
                domainClass, 
                bindingSource,
                convertToListIfString(includeExclude.get('include')),
                convertToListIfString(includeExclude.get('exclude')), 
                filter);
        return domainClass;
    }
    

    convertToListIfString is as per the controller method:

    @SuppressWarnings("unchecked")
    private List convertToListIfString(Object o) {
        if (o instanceof CharSequence) {
            List list = new ArrayList();
            list.add(o instanceof String ? o : o.toString());
            o = list;
        }
        return (List) o;
    }
    

提交回复
热议问题