Reputation: 185
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 associations, so it has to work with objects too.
I would like not to set properties (their names) to object, which values are null.
Upvotes: 1
Views: 7959
Reputation: 51
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...
Upvotes: 3
Reputation: 827
Is this what you want to do?
obj.properties = params
Hope that helps
Upvotes: 0
Reputation: 187499
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.
Upvotes: 2