Grails 中的 URL 映射问题:参数不正确

发布于 2024-11-28 03:45:02 字数 692 浏览 1 评论 0原文

我有一些控制器操作需要几种不同的 HTTP 方法,例如 GET 和 POST。我没有在控制器操作代码中处理这个问题,而是决定(错误地)如果将此代码放入 UrlMappings.groovy 类中,它会更快且更简单。

这是我到目前为止所得到的:

class UrlMappings {

    static mappings = {

        ...

        "/$controller/(create|edit)/$id" {
            action = [
              GET: "editView",
              POST: "edit"
            ]
        }
    }
}

因此,在每个控制器中,如果第二个 URL 参数与“edit”匹配,则用户将被转发到两个操作之一,具体取决于请求的 HTTP 方法。

一切正常,直到代码到达我的 editViewedit 操作,其中此代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-12-05 03:45:02

我认为您不能像现在这样使用 URL 映射语法,您需要将其分成两个映射,例如:

    "/$controller/edit/$id" {
        action = [
            GET: "editView",
            POST: "edit"
        ]
    }

    "/$controller/create/$id" {
        action = [
          GET: "editView",
          POST: "edit"
        ]
    }

意识到这会重复操作块,但我认为没有办法解决这不是为你的行动添加另一个变量并据此决定要做什么。

抱歉,如果我误解了您 - 这本来是一条评论,但我还不能发表任何评论!

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:

    "/$controller/edit/$id" {
        action = [
            GET: "editView",
            POST: "edit"
        ]
    }

and

    "/$controller/create/$id" {
        action = [
          GET: "editView",
          POST: "edit"
        ]
    }

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文