Grails 控制器中操作和方法之间的差异 [英] Differences between action and methods in Grails controllers

查看:16
本文介绍了Grails 控制器中操作和方法之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,如果我想在控制器中创建一个动作,那么我可以这样做:

As far as I know, if I want to create an action in a controller then I can do it by:

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

或者我可以通过以下方式创建它:

or I can create it by:

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

谁能告诉我这两种方法的区别,或者我的概念或看法有什么问题

Can somebody please tell the difference between the two methodology , or if I have anything wrong with my concept or perception

推荐答案

第一个实现是在控制器中定义公共闭包,第二个是使用公共方法.

The first implementation was defining public closures in the controller, the second is to use public methods.

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

The second way was introduced in grails 2, and is widely considered to be the best way.

我能从脑海中想到几个原因:

A couple of reasons i can think of from the top of my head:

  • 特别是在最新版本的 grails 中,使用方法可以让您利用 Traits.
  • 您可以使用继承来组织您的方法
  • 它必须更有效率

更新:为什么要使用 grails 操作被声明为方法而不是闭包,它有什么区别?

这篇关于Grails 控制器中操作和方法之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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