Search code examples
grailsmethodscontrollerclosuresaction

Differences between action and methods in Grails controllers


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


Solution

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

    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:

    • Especially in recent versions of grails, using methods allows you to take advantage of Traits.
    • You can use inheritance to organize your methods
    • it has to be more efficient

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