1

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

lucke84
  • 4,516
  • 3
  • 37
  • 58
  • 1
    possible duplicate of [Why should grails actions be declared as methods instead of closures and what difference does it make?](http://stackoverflow.com/questions/9205209/why-should-grails-actions-be-declared-as-methods-instead-of-closures-and-what-di) –  Mar 24 '13 at 20:59

3 Answers3

2

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?

Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
  • 2
    `@Secured` does work on closure actions as well as method actions (at least when using the `spring-security-core` Grails plugin). – Ian Roberts Mar 25 '13 at 18:53
0

I'm not sure, but I think that it used to be the first way was the only way to do it, and then it changed to allow the second way. I think that the second way is the preffered way, but I'm not sure.

[Edit]

Here is another stack overflow post further explaining it:

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

Community
  • 1
  • 1
Katniir
  • 41
  • 6
0

Leveraging methods instead of actions(Closure properties) has some advantages:

  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.

Please visit following link for more information.

Grails controllers

Pankaj Shinde
  • 3,361
  • 2
  • 35
  • 44