Monday, 17 August 2009

Dynamic UrlMapping using request parameters

Might be old news to some, but I just found you can assign closures to the action and controller values in URL mappings, e.g.

"/update/$controller/$target" {
action = {
"update" + params.target[0].toUpperCase() + params.target.substring(1);
}
}

Great for mapping restful style urls to meaningful action names, e.g.

"/update/patient/speciality" => PatientController.updateSpeciality

2 comments:

Rob said...

a RESTful URL wouldn't specify the action type in the URL, it would be implied by the HTTP method. Then you can use either "postRest" or "parseRequest" in your URLMappings to dispatch to the appropriate actions. In this case:

"/patient/speciality"(controller: "patient", parseRequest: true) {
action = [POST: "updateSpeciality"//, etc.]
}

Stephen Cresswell said...

Wondered who would pick up on that. It's the reason I wrote "restful style" url rather than RESTful URL, but I guess I should have written "meaningful" instead.

Personally I prefer seeing the action in the url and because of IE caching problems with AJAX GET requests I often resort to using POST when I should really use GET. This means the action type can't be implied from the HTTP method.