Differences between action and methods in Grails controllers

痴心易碎 提交于 2019-12-02 04:01:35

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?

Catlin

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?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!