【问题标题】:Grails 2.2.0 URLMappings: Any way to use same URL with Different VerbGrails 2.2.0 URLMappings:使用不同动词的相同 URL 的任何方式
【发布时间】:2012-12-22 03:45:11
【问题描述】:

我有以下几点:

"/api/users"(controller: "user") {
   action = [GET:"list"]
}

打电话给http://localhost:8080/platform/users 我得到了一个用户列表。然后我添加了这个:

"/api/users"(controller: "user") {
   action = [POST:"save"]
}

现在我得到一个 404 并且它没有命中 UserController 中的任何一个方法。我希望能够使用与控制哪个动作的动词相同的 URL。是我做错了还是 Grails 不支持这个?

【问题讨论】:

    标签: grails url-mapping


    【解决方案1】:

    来自 Grails 文档:URL Mappings

    static mappings = {
       "/product/$id"(controller:"product") {
           action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"]
       }
    }
    

    对于您的情况:

    "/api/users"(controller: "user") {
       action = [GET:"list",POST:"save"]
    }
    

    【讨论】:

    • 我觉得自己像个白痴。我知道。必须..得到..睡觉。谢谢詹姆斯。
    • 如果方法在不同的控制器中呢?或者例如 /users 执行一项操作而 /users/1111 执行另一项操作?
    • @Jackie 可能你的应用程序结构计划不好。如果更复杂,还请记住重定向。
    【解决方案2】:

    检查你的用户控制器,看看是否有这样定义的 allowedMethods:

    class UserController {
    
        static allowedMethods = [save: "POST", list: "GET"]
    
        def list() {
        .....
        }
    
        def save() {
        .....
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 2015-03-19
      • 1970-01-01
      相关资源
      最近更新 更多