Jackie
Jackie

Reputation: 23519

How to use path params in Grails 2.3.7

I have a project using grails and I would like my controller url to look like

http://<domain>/<contextRoot>/<controller>/<method>/123 

where 123 is the ID of an object. I can do this in Spring like...

@RequestMapping(value = "/path/to/{iconId}", method = RequestMethod.GET) 

But since the annotations are handled by Grails I am not sure how to override them.

Upvotes: 0

Views: 502

Answers (2)

Joshua Moore
Joshua Moore

Reputation: 24776

URL mappings are done within grails-app/conf/UrlMappings.groovy The documentation can explain in further details what you can do but for your example you can do the following:

class UrlMappings {
    static mappings = {
        "/path/to/${iconId}"(controller: 'myController', action: 'myAction')
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

Take note that the mappings are first match basis, so you are adding a static path to a controller and action. Your iconId will be exposed automatically in the parameters as params.iconId

Upvotes: 1

D&#243;nal
D&#243;nal

Reputation: 187529

Add the following to UrlMappings.groovy

"/$controller/$action?/$id?"

Upvotes: 1

Related Questions