Grails:我们可以在g:link中将字符串作为id传递吗?还是其他方式?

Grails:我们可以在g:link中将字符串作为id传递吗?还是其他方式?,grails,grails-2.0,grails-controller,Grails,Grails 2.0,Grails Controller,场景: 我想创建g:link,它将重定向到 www.xyz.com/controller/action/title_of_question 相反 www.xyz.com/controller/action/id_of_question 我知道,通过配置g:link的idparam,可以很容易地创建后面的一个 请注意,问题的标题是用连字符(-)分隔的字符串 有人能告诉我如何实现它吗?我认为最好的办法是在控制器中创建一个新操作,该操作会获取您的ID,并将您的ID重定向到您试图转到的URL <

场景:
我想创建
g:link
,它将重定向到

www.xyz.com/controller/action/title_of_question
相反

www.xyz.com/controller/action/id_of_question
我知道,通过配置
g:link
id
param,可以很容易地创建后面的一个 请注意,问题的标题是用连字符(-)分隔的字符串
有人能告诉我如何实现它吗?

我认为最好的办法是在控制器中创建一个新操作,该操作会获取您的ID,并将您的ID重定向到您试图转到的URL

<g:link resource="book" action="name" id="${id}">New Book</g:link>

我建议采取以下措施:

def someAction() {
    if(params.id.matches(/[0-9]+/)) {
        //logic for id of question
    } else {
        //you could check if it is separated with dashes
        //logic for title of question
    }
}
这将触发id逻辑:

www.xyz.com/controller/someAction/15 
这就是“标题”逻辑:

www.xyz.com/controller/someAction/title-of-question

简短回答:是的,我们可以:)

如果您在
UrlMappings.groovy
中没有将
id
定义为
Long
Integer
的规则,您可以创建带有
String
标识符的链接,如下所示:

<g:link controller="controllerName" action="show" id="someName">Link!</g:link>
<g:link controller="controllerName" action="show" id="someName">Link!</g:link>
def show(String id) {
    ...
}