开发者

grails how to iterate through params and set values

开发者 https://www.devze.com 2023-03-06 09:13 出处:网络
Is there anybody who has written a universal action for iterating through all params values and setting these values on an object?

Is there anybody who has written a universal action for iterating through all params values and setting these values on an object?

I want to write something like this:

def updateSomeObject = {obj->
    for (def key : params.keySet()) {
        if (obj.hasProperty(key) != null) {
            def strValue = params[key]
            obj[key] = strValue
    }
}

but this works only for String values. In my case there are one to one associat开发者_运维问答ions, so it has to work with objects too.

I would like not to set properties (their names) to object, which values are null.


I use this to loop grails params:

Collection<?> keys = params.keySet()
    for (Object key : keys) {
        //check if key=action and key=controller which is grails default params
        if (!key.equals("action") && !key.equals("controller")) {
            println key //print out params-name
            println params.get(key) //print out params-value
        }
    }

Hope that help...


It looks like you're trying to bind request parameters to an object. You really shouldn't need to write your own code to do this, as the Grails controllers provide a bindData method that does this already.


Is this what you want to do?

obj.properties = params

Hope that helps

0

精彩评论

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