Question

This is probably a simple one and more Java related than grails but I'm a bit lost and not sure where to even start looking on this, I've googled about but am not really sure what I'm after, so would appreciate a pointer if possible please!

In the grails app I have a form which I save, all well and good. In the controller I can see the list of params it returns via a simple println and when I want to find a specific value currently I do a params.each and then compare the key to a pre defined string to find the one I want, my question is: -

Can I, and how would I, specifically say "get me the value of the parameter with the key "banana", rather than having to loop through the whole list to find it?

Also is there a way of creating a new set of secondary params, or just another plain old dictionary item (is that the right term?) where I use a regular expression to say "give me all the items whose key match the pattern "XYZ"?

It probably doesn't make much difference speed wise as the params are never that big but it'd be nice to make things more efficient where possible.

Any feedback much appreciated!

Was it helpful?

Solution

For a first question, to get 'banana' parameter you have to use:

params.banana

For second, find all with regexp:

def matched = params.findAll { it.key =~ /XYZ/ }
//or
Pattern p = ~/XYZ/
def matched = params.findAll { p.matcher(it.key).matches() }

OTHER TIPS

There's a params object you can use. Eg with someurl.com?myparam=test you can access it with "params.myparam"

More information over here: http://grails.org/doc/2.2.x/ref/Controllers/params.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top