参数 'id' 是 Grails 重定向中的查询字符串
Parameter 'id' to be query string in Grails redirection
从 Grails 控制器,我想重定向到这个(使用查询字符串):
/mycontroller/myaction?id=3
我写了这段代码:
def a = 3;
redirect (controller:"mycontroller",action:"myaction",params:[id:a])
此代码将生成:
/mycontroller/myaction/3
我知道'id'是特殊参数。它将是 url 而不是查询字符串。我尝试另一个参数
def name = "John";
redirect (controller:"mycontroller",action:"myaction",params:[name:name])
将产生:
/mycontroller/myaction?name=John
您描述的行为是您 UrlMappings 配置的结果。
如果使用默认映射,id
参数将放在描述的位置 $id?
:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
一般来说这是没有问题的。您还可以使用 id
参数,就好像它被设置为查询字符串一样:
def myaction() {
def idFromParams = params.id
}
或者您只需重写 UrlMappings
。
从 Grails 控制器,我想重定向到这个(使用查询字符串):
/mycontroller/myaction?id=3
我写了这段代码:
def a = 3;
redirect (controller:"mycontroller",action:"myaction",params:[id:a])
此代码将生成:
/mycontroller/myaction/3
我知道'id'是特殊参数。它将是 url 而不是查询字符串。我尝试另一个参数
def name = "John";
redirect (controller:"mycontroller",action:"myaction",params:[name:name])
将产生:
/mycontroller/myaction?name=John
您描述的行为是您 UrlMappings 配置的结果。
如果使用默认映射,id
参数将放在描述的位置 $id?
:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
一般来说这是没有问题的。您还可以使用 id
参数,就好像它被设置为查询字符串一样:
def myaction() {
def idFromParams = params.id
}
或者您只需重写 UrlMappings
。