为什么 grails 动作应该被声明为方法而不是闭包,它有什么区别? [英] Why should grails actions be declared as methods instead of closures and what difference does it make?

查看:12
本文介绍了为什么 grails 动作应该被声明为方法而不是闭包,它有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 grails 2.0 中有什么新内容 http://grails.org/doc/2.0.0.RC1/guide/introduction.html#whatsNew
它说:
1.1.3 网页功能控制器动作作为方法现在可以将控制器操作定义为方法,而不是像以前的 Grails 版本那样使用闭包.事实上,这是现在表达动作的首选方式.
例如:

In grails 2.0 whats new http://grails.org/doc/2.0.0.RC1/guide/introduction.html#whatsNew
it says:
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.
For example:

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

}

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

Why is this important? What difference does it make?

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

UPDATE: I found this discussion that talks a lot about scope and some pretty hairy stuff. http://grails.1312388.n4.nabble.com/Controller-actions-methods-or-closures-was-Re-grails-dev-Statically-typed-meta-programing-td3048287.html

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

I guess my question could also be this: what advantage do closures have for the actions?

推荐答案

答案是这里

来自上面的链接

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

Leveraging methods instead of Closure properties has some advantages:

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

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

Also there's a similar groovy question here that has some more details

这篇关于为什么 grails 动作应该被声明为方法而不是闭包,它有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆