【问题标题】:how redirect and respond of save action works in grails 2.3.0 controller保存操作的重定向和响应如何在 grails 2.3.0 控制器中工作
【发布时间】:2014-05-10 16:25:15
【问题描述】:
request.withFormat {
            形式 {
                重定向humanInstance
                //解释 : old grails redirect like redirect(action: "show", id: humanInstance.id)
                //但是在 2.3.0 中通过重定向 humanInstance 这个语句我看到它转到 def show(Human humanInstance) 动作
                //Q1:它如何理解必须重定向 def show(Human humanInstance) 动作?
                //因为这里有这么多接收humanInstance的方法
                //Q2: 它在哪里处理?
            }
            '*' {
                响应humanInstance,[状态:已创建]
            }
            //说明:我在上面测试过 {works for normalCall}'*'{works for restCall}
            //Q3: 什么是'*'操作符在这里做什么?
        }

【问题讨论】:

    标签: grails controller


    【解决方案1】:

    '*' 是一种包罗万象的方法,它与一个代码块相关联,如果没有其他块与接受标头匹配,则该代码块应执行。请参阅以下内容...

    request.withFormat {
        xml {
            // execute this if the request content type is xml
        }
        json {
            // execute this if the request content type is json
        }
        '*' {
            // execute this if neither of the other 2 matched the request's content type
        }
    }
    

    您可能想了解很多内容,包括您已配置的 mime 类型。您应该查看http://grails.org/doc/2.3.x/guide/theWebLayer.html#contentNegotiation 官方用户指南的内容协商部分。

    希望对你有帮助。

    更新:

    当您调用 redirect(someDomainClassInstance) 时,您最终会到达 https://github.com/grails/grails-core/blob/v2.3.8/grails-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/plugins/web/api/ControllersApi.java#L270

    调用https://github.com/grails/grails-core/blob/v2.3.8/grails-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/plugins/web/api/ControllersApi.java#L248

    调用https://github.com/grails/grails-core/blob/v2.3.8/grails-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/web/metaclass/RedirectDynamicMethod.java#L160

    调用https://github.com/grails/grails-core/blob/v2.3.8/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultLinkGenerator.groovy#L175

    此时“方法”的值为“GET_ID”。 https://github.com/grails/grails-core/blob/v2.3.8/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultLinkGenerator.groovy#L56 中与 GET_ID 关联的值是“show”,这表明您将重定向到 show 方法。

    【讨论】:

    • 感谢 Jeff,Q3 的回答很好,你能回答 Q1 和 Q2
    • 答案太长,不适合此评论,因此我添加了另一个答案。我已经看到了你的几个问题,它们很难回答,因为你把几个问题放在一篇文章中,大部分都是不连贯的,而且连贯的部分由于不连贯的部分而不必要地复杂化,这意味着它需要付出比实际应该更多的努力为您提供有用的信息。如果你不把它们混为一谈,让它们更简单,你的问题会更容易回答。
    • 我删除了第二个答案并将其内容粘贴到第一个答案中,该答案已被接受。我认为这将使这个问题更容易查看。
    • 我认为答案中“更新”之后的所有内容现在都针对 Q1 和 Q2。你真的不应该像这样把这些问题捆绑在一起。它使事情不必要地复杂化。
    猜你喜欢
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多