URL Mappings
Site Admin
· 11 Sep 2026
· 7 views
URL Mappings Configuration
URL mappings in Grails define how URLs map to controller actions. They are configured in grails-app/controllers/.../UrlMappings.groovy and provide a powerful way to customize routing.
Basic Mappings
class UrlMappings {
static mappings = {
"/"(controller: "home", action: "index")
"/book/$id"(controller: "book", action: "show")
"/books"(controller: "book", action: "list")
}
}
Dynamic Path Variables
You can capture URL segments as parameters using the dollar sign syntax:
"/book/$id"(controller: "book", action: "show")
// Multiple segments
"/author/$authorId/book/$bookId"(controller: "book", action: "showByAuthor")
Constraints and Filters
URL mappings support constraints to restrict which URLs are matched:
"/book/$id"(controller: "book", action: "show") {
constraints {
id matches: /[0-9]+/
}
}
RESTful Mappings
Grails provides shorthand methods for RESTful routes:
resources "book"
This single line creates mappings for index, show, create, update, and delete actions following REST conventions.
Error Handling
The 404 and 500 mappings handle pages not found and server errors respectively:
"404"(controller: "errors", action: "notFound")
"500"(controller: "errors", action: "serverError")
Key Points
- URL mappings are defined in
UrlMappings.groovy. - Use dollar sign syntax for dynamic path variables.
- Constraints restrict URL matching patterns.
resourcescreates RESTful route shorthands.- Custom error pages handle 404 and 500 responses.