gpt4 book ai didi

grails - 用于休息和其他操作的Grails UrlMappings

转载 作者:行者123 更新时间:2023-12-02 14:41:26 24 4
gpt4 key购买 nike

我有一个 Controller ,该 Controller 主要用于使用showupdatesavedelete操作的REST通信。相应地将其映射到UrlMappings.groovy文件中,并且工作正常。

然后,我需要在同一 Controller 中调用getAccountTypesByEnv Action ,但是我在设置实际上有效的语法时遇到了一些麻烦。

下面的定义有效,但是我想知道是否存在更简单,更正确的方法。

"/ext/accounttype/$id?"(controller: "accountType") {
action = [GET: 'show', PUT: 'update', POST: 'save', DELETE: 'delete']
"/ext/accounttype/getAccountTypesByEnv"(controller: "accountType", action: "getAccountTypesByEnv")
}

更新

我最终将其分为两个单独的通用映射,如下所示:
"/ext/$controller/$id?" {
action = [GET: 'show', PUT: 'update', POST: 'save', DELETE: 'delete']
}

"/ext/$controller/action/$customAction?" {
action = { return params.customAction }
}

最佳答案

您可以像这样在UrlMappings.groovy中定义:

 "/ext/$controller/$resourceId?/$customAction?" {
action = {
Map actionMethodMap = [GET: params.resourceId ? "show" : "index", POST: "save", PUT: "update", DELETE: "delete"]

return params.customAction ?: actionMethodMap[request.method.toUpperCase()]
}
id = {
if (params.resourceId == "action") {
return params.id
}
return params.resourceId
}
}

这是我目前认为最通用的 UrlMapping,您可以通过以下方式在此处使用它:
GET     "/ext/accounttype" will call **index** action of AccounttypeController
POST "/ext/accounttype" will call save action of AccounttypeController
PUT "/ext/accounttype/14" will call update action of AccounttypeController with id 14
DELETE "/ext/accounttype/14" will call update action of AccounttypeController with id 14
GET "/ext/accounttype/14" will call show action of AccounttypeController with id 14
GET "/ext/accounttype/action/getAccountTypesByEnv" will call getAccountTypesByEnv action of AccounttypeController with null id
GET "/ext/accounttype/action/getAccountTypesByEnv?id=143" will call getAccountTypesByEnv action of AccounttypeController with id 143

关于grails - 用于休息和其他操作的Grails UrlMappings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33430613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com