8.3.1 Mapping to Controllers and Actions

To create a simple mapping simply use a relative URL as the method name and specify named parameters for the controller and action to map to:

  1. "/product"(controller: "product", action: "list")

In this case we’ve mapped the URL /product to the list action of the ProductController. Omit the action definition to map to the default action of the controller:

  1. "/product"(controller: "product")

An alternative syntax is to assign the controller and action to use within a block passed to the method:

  1. "/product" {
  2. controller = "product"
  3. action = "list"
  4. }

Which syntax you use is largely dependent on personal preference.

If you have mappings that all fall under a particular path you can group mappings with the group method:

  1. group "/product", {
  2. "/apple"(controller:"product", id:"apple")
  3. "/htc"(controller:"product", id:"htc")
  4. }

You can also create nested group url mappings:

  1. group "/store", {
  2. group "/product", {
  3. "/$id"(controller:"product")
  4. }
  5. }

To rewrite one URI onto another explicit URI (rather than a controller/action pair) do something like this:

  1. "/hello"(uri: "/hello.dispatch")

Rewriting specific URIs is often useful when integrating with other frameworks.