Grails SEO友好URL

Grails SEO友好URL,url,grails,seo,url-mapping,Url,Grails,Seo,Url Mapping,在grails中创建URL的标准方法是: <a href="${createLink(controller:'news', action: 'show', params: [id: news.id])}">${news.title}</a> 在Grails中,最干净的方法是什么?我可以使用grails URLMapping将/news/show/102映射到/news/102,但是如何创建如上所述的完整URL?您可以将标题转换为如下参数: name story: "/n

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

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

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

您可以将标题转换为如下参数:

name story: "/news/$id/$headline" {
    controller = "news"
    action = "show"
}
这样,您可以创建包含标题的URL,并且映射仍然有效。当然,您实际上不必使用将出现在控制器中的headline参数。上面的示例使用命名URL映射,因此您可以说:

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

您可能还对这个创建规范URL的插件感兴趣-

Dave谢谢!此方法对于多语言站点非常方便,在这些站点中,您可以根据当前区域设置以特定的映射器为目标。在我的示例中,我有story en和story hr映射,在我的g:link中,我使用映射:“story-${locale}”,它为所选区域选择合适的映射器。
${createLink(mapping: "story", params: [id: 102, headline: 'this-is-the-hottest-news-today'])}