1
votes

I want little help which I suspect is due to my lack of understanding for Groovy syntax. So, here's the thing:

On the GSP page I want to set a field's value from the params map which is

 ["id":"107901", "Field_10.value":"2", "Field_10":["value":"2"],"Field_11.value":"", "Field_11":["value":""],action:'abc']

On the gsp page, I want to find the value against the key Field_{some-id}.value

So I am calling a tag like, g.testTag(id:field.id) with its implementation as

def testTag = { attrs,body->

    println "params are ${params}"
    def result = ""
    def keyRequired = "Field_${attrs.id}.value"
    println "keyRequired >>>>> ${keyRequired.toString()}"
    params.each { key,value->
        println "key is ${key}"
        println "Value is ${value}"
        if (key.equals(keyRequired.toString())) {
            result = params.value
        }
    }
    println "Final result is >>>>>> ${result}"
}

The value passed in id is 10 and with my params printed as above, I was expecting a value of 2 which is corresponding to the key in the params to show up. But apparently I see the result as null..

What am I doing wrong ? Can anyone help please...

Thanks

3

3 Answers

2
votes

Not result = params.value, but result = value.

1
votes

You have to change the line:

result = params.value

to:

result = value

At the each loop, you're basically saying that inside the params iteration, you're naming every key "key" and every value "value". So, params.value will actually look for the key value inside your params map, which is null.

Funny that you do that right with key but not with value. Probably just got distracted.

1
votes

it is likely what you want to do, the groovy way (no need to loop over the keys of the map) to access "Field_10.value":"2"

result=params["Field_${attrs.id}.value"]

Alternatively, this also works because you have "Field_10":["value":"2"] in your map

result=params["Field_${attrs.id}"].value