Reputation: 1846
In one place in my system I do the following, which works correctly:
log.debug params."${tagType}"_${i}" //resolves to, e.g : params.title_0
Furthermore, the following also works:
log.debug params."${'setTagtypesList[0].tagtype.id'} // hard coded 0 index
(Note that above resolves to params.'setTagtypesList[0].tagtype.id'
and that the single quotes are necessary.)
However the params request that follows results in NULL:
def someInt = 0
log.debug params."'setTagtypesList[someInt].tagtype.id'" //dynamic index
So how can I dynamically create a param name that contains array syntax? I'm using Grails 1.3.9.
Upvotes: 4
Views: 1037
Reputation: 122364
There are various ways to achieve what you want, including:
params."setTagtypesList[${someInt}].tagtype.id"
params['setTagtypesList[' + someInt + '].tagtype.id']
Both of which are ultimately shorthand for
params.get('setTagtypesList[' + someInt + '].tagtype.id')
The thing to remember with GStrings is that anything inside ${}
is a Groovy expression, anything outside the braces is taken literally.
However note that
params["setTagtypesList[${someInt}].tagtype.id"]
Would likely not work, because it is looking up a map entry with a GString key rather than a String. The property access dot notation does an implicit toString()
on the key before looking it up, this is one of the cases where you do need to be aware of the difference between Strings and GStrings.
Upvotes: 3