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

查看:253
本文介绍了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 "
    }
}

或者我可以创建它:

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


$ b b

有人可以告诉两种方法之间的区别,或者如果我的概念或感知有任何问题。

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:


  • 使用标准AOP技术无法实现Closures上的注释(如@Secured),因此需要古怪的groovy黑魔法。

  • 您可以使用继承

更新:

为什么grails操作应该声明为方法而不是闭包,它有什么区别?

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

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