【问题标题】:Why should grails actions be declared as methods instead of closures and what difference does it make?为什么要将 grails 操作声明为方法而不是闭包,它有什么区别?
【发布时间】:2012-03-01 14:09:34
【问题描述】:

在 grails 2.0 中有什么新的 http://grails.org/doc/2.0.0.RC1/guide/introduction.html#whatsNew
它说:
1.1.3 Web Features Controller Actions as Methods It is now possible to define controller actions as methods instead of using closures as in previous versions of Grails. In fact this is now the preferred way of expressing an action.
例如:

// action as a method
def index() {
}
// action as a closure
def index = {

}

为什么这很重要?有什么区别?

更新:我发现这个讨论讨论了很多关于范围和一些非常多毛的东西。 http://grails.1312388.n4.nabble.com/Controller-actions-methods-or-closures-was-Re-grails-dev-Statically-typed-meta-programing-td3048287.html

我想我的问题也可能是这样的:闭包对操作有什么好处?

【问题讨论】:

    标签: grails groovy closures


    【解决方案1】:

    答案是here

    从上面的链接

    利用方法而不是闭包属性有一些优点:

    1. 内存高效
    2. 允许使用无状态控制器(单例范围)
    3. 您可以覆盖子类中的操作并使用 super.actionName() 调用被覆盖的超类方法
    4. 可以使用标准代理机制拦截方法,这对于闭包来说很复杂,因为它们是字段。

    还有一个类似的 groovy question here 有更多细节

    【讨论】:

    • 很有趣,那么为什么它们会关闭呢?
    猜你喜欢
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多