Grails 中的 URL 映射问题:参数不正确
我有一些控制器操作需要几种不同的 HTTP 方法,例如 GET 和 POST。我没有在控制器操作代码中处理这个问题,而是决定(错误地)如果将此代码放入 UrlMappings.groovy 类中,它会更快且更简单。
这是我到目前为止所得到的:
class UrlMappings {
static mappings = {
...
"/$controller/(create|edit)/$id" {
action = [
GET: "editView",
POST: "edit"
]
}
}
}
因此,在每个控制器中,如果第二个 URL 参数与“edit”匹配,则用户将被转发到两个操作之一,具体取决于请求的 HTTP 方法。
一切正常,直到代码到达我的 editView
或 edit
操作,其中此代码:
params.id
计算为 edit
,而不是 1 正如此示例请求所预期的那样:
/location/edit/1
。
这是 Grails 中的错误吗?
I have a few controller actions that require several different HTTP methods, GET and POST for example. Instead of handling this in the controller action code, I decided (incorrectly) that it would be faster and less complex if I put this code into the UrlMappings.groovy class.
Here is what I have so far:
class UrlMappings {
static mappings = {
...
"/$controller/(create|edit)/$id" {
action = [
GET: "editView",
POST: "edit"
]
}
}
}
So, in every controller, if the second URL parameter matches "edit", the user will be forwarded to one of two actions depending on the HTTP method of the request.
Everything works fine until the code reaches my editView
or edit
action where this code:
params.id
evaluates as edit
, instead of as 1
as expected from this example request: /location/edit/1
.
Is this a bug in Grails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不能像现在这样使用 URL 映射语法,您需要将其分成两个映射,例如:
我
意识到这会重复操作块,但我认为没有办法解决这不是为你的行动添加另一个变量并据此决定要做什么。
抱歉,如果我误解了您 - 这本来是一条评论,但我还不能发表任何评论!
I don't think you can use the URL mapping Syntax the way that you are, you would need to split it into two mappings something like:
and
I realise this duplicates the action block, but I don't think there is a way around this other than putting another variable in for your action and deciding what to do based on that.
Sorry if I've misunderstood you - this would have been a comment, but I'm not allowed to post any yet!