从Grails Controller ,我想重定向到此(使用查询字符串):
/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 - 参数 'id'是Grails重定向中的查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472811/