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

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

问题描述

在grails 2.0中,新增了 http://grails.org/doc/2.0。 0.RC1 / guide / introduction.html#whatsNew
它说:

1.1.3 Web功能
控制器操作作为方法
现在可以将控制器操作定义为方法,而不是像以前的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?

推荐答案

答案是这里

从上面的链接

使用方法代替Closure属性有一些优点:

Leveraging methods instead of Closure properties has some advantages:


  1. 内存高效

  2. 允许使用无状态控制器(单例范围)

  3. 您可以覆盖子类的操作,并调用覆盖的超类方法super.actionName()

  4. 可以使用标准代理机制拦截方法,这是Closures因为它们是字段而复杂的。

  1. Memory efficient
  2. Allow use of stateless controllers (singleton scope)
  3. You can override actions from subclasses and call the overridden superclass method with super.actionName()
  4. Methods can be intercepted with standard proxying mechanisms, something that is complicated to do with Closures since they're fields.

此外,还有一个类似的问题,这里有一些更多的细节

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

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

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