6

在 grails 中创建 URL 的标准方法是:

<a href="${createLink(controller:'news', action: 'show', params: [id: news.id])}">${news.title}</a>

生成网址:/news/show/102

我想要更多 SEO 友好的 URL,例如:

/news/102/this-is-the-hottest-news-today

在 Grails 中最干净的方法是什么?我可以使用 grails URLMapping 映射/news/show/102/news/102,但我如何创建像上面这样的完整 URL?

4

1 回答 1

15

您可以将标题变成这样的参数:

name story: "/news/$id/$headline" {
    controller = "news"
    action = "show"
}

这样,您可以创建带有标题的网址,并且映射仍然有效。当然,您实际上不必使用将出现在控制器中的标题参数。上面的例子使用了一个命名的 URL 映射,所以你可以说:

${createLink(mapping: "story", params: [id: 102, headline: 'this-is-the-hottest-news-today'])}

您可能还对这个用于创建规范 url 的插件感兴趣 - http://www.grails.org/plugin/canonical

于 2010-03-02T09:31:09.710 回答