Include name in Grails URL

In my Grails application, I have a couple of domain classes, say Authorand Book. I use the default URL mappings:

static mappings = {
  "/$controller/$action?/$id?"{
    constraints {
      // apply constraints here
    }
  }
}

So the relative url to show the book with id 2 is /book/show/2. To show the author with id 5 it /author/show/5. Both the class Authorand Bookhave the property name, but it is not guaranteed to be unique.

For SEO reasons, I would like to include this name in these URLs, for example. change the url to show the book to /book/show/2/the+davinci+codeand the url to show the author /author/show/5/dan+brown.

In order not to violate the existing (external) links to my pages, I would ideally want to support both formats, so that either of the following actions displays a Dan Brown page

  • /author/show/5
  • /author/show/5/dan+brown

, (URL ), (URL- )?

+3
2

,

. -, ( , ), URL- :

static mappings = {
  "/$controller/$action?/$id?/$extra?"{
    constraints {
      // apply constraints here
    }
  }
}

. Author Book, UrlMappings.groovy

static mappings = {
    "/author/show/$id?/$name?" (controller:"author", action: "show")
    "/book/show/$id?/$name?" (controller:"book", action: "show")
    "/$controller/$action?/$id?" {
          constraints {
          // apply constraints here
          }
    }
}

URL-, "/author/show/10/dan + brown", "/author/show/10", show params.name.

+1

, :

"/author/show/$id/$name"{
    constraints {
        controller='author'
        action='show'
    }
}

id,

def show(id, name) {

}
0

All Articles