Wednesday, March 10, 2010

Grails Goodness: Get Values from Parameters with Same Name

Grails supports type conversion on request parameters. But it is also easy to handle multiple request parameters with the same name in Grails. We use the list() method on the params and we are sure we get back a list with values. Even if only one parameter with the given name is returned we get back a list.

// File: grails-app/controllers/SimpleController.groovy
class SimpleController {

    def test = {
        // Handles a query like 'http://localhost:8080/simple?role=admin&role=user'
        def roles = params.list('role')
        render roles.join(',')  // Returns admin,user



Edvinas said...

If we're having params coming from PSOT data, can we trust that it will be sorted just like it was sent?

jakob.kuelzer said...

Wow, most useful. Can't remember how many times I've written something like this to handle checkbox/multiple select logic.
Thank you very much and keep up the good work. :)

Amit said...

Thanks!! It was really helpful

twicet said...

Thanks for highlighting the list() method. I had been pulling my hair trying to access parameters sent from jquery having names of the form "parameterName[]". The list() method turned out to be what the doctor ordered.

chichibek said...

thanks for the info. most useful

Post a Comment