据我所知,如果我想在 Controller 中创建一个 Action ,那么我可以通过以下方式完成:

class My Controller { 
    def myAction = { 
      println "in my action " 
    } 
} 

或者我可以通过以下方式创建它:
class My Controller { 
    def myAction(){ 
      println "in my action " 
    } 
} 

有人可以告诉我这两种方法之间的区别,或者我的概念或看法有什么问题吗?

请您参考如下方法:

第一个实现是在 Controller 中定义公共(public)闭包,第二个是使用公共(public)方法。

第二种方式是在 grails 2 中引入的,被广泛认为是最好的方式。

我能想到的几个原因:

  • 尤其是在最新版本的 grails 中,使用方法可以让您利用 Traits。
  • 您可以使用继承来组织您的方法
  • 它必须更高效

  • 更新:
    Why should grails actions be declared as methods instead of closures and what difference does it make?


    评论关闭
    IT序号网

    微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!