dr jerry
dr jerry

Reputation: 10026

Where do all methods, attributes get wired into grails controller?

I'm looking at grails (2.2), and it's all beautiful and even magical, how it all works. I'm looking at a Controller class which is created with grails create-controller and out of the box it has many methods and properties available, like render(), redirect(), params, request and I presume it goes on and on. where does all this get wired in? Where in the code/project/framework do I see that render() is made available as a method? And how is it implemented? As a java developer I'm used to inheritance and code injection and reflection. And in javascript prototypes can do some black magic. But the XXController.groovy is just a standalone object. Is it the name (XXController) or the location (grails-app/controllers?) or is there some injection happening which the IDE can pick up?

Upvotes: 1

Views: 203

Answers (2)

Burt Beckwith
Burt Beckwith

Reputation: 75681

As of Grails 2.0+ it's implemented using an AST transformation - previously it was done by adding the methods to the Groovy MetaClass. The benefits of the new approach are that things will be a bit faster and use less memory.

GORM domain class methods now use this approach too (except for dynamic methods like findByFooAndBar which have to be added dynamically to the metaclass) and those have the benefit of being callable from Java since the AST adds the methods to the bytecode. This doesn't help controllers though since they're only called from Grails itself as the result of a web request.

For the gory details, ControllersApi is where the methods are, and they're mixed into each controller class by a combination of ControllerTransformer and the code in the doWithDynamicMethods closure in ControllersGrailsPlugin

Upvotes: 1

David Santamaria
David Santamaria

Reputation: 8821

Welcome to the wonderfull world of Grails, Here you have a couple of links that may help you:

  • The Section of Controllers in the Web Layer docs.
  • And the docs of the render method. Check it out the "Quick reference" column at the right for more methods avaliable at the Controllers.

If you are wondering how that magic is done, Grails is an open source project, so as usual, go and serve yourself at Github (warning, it is quite large project).

Grails works on the top of Groovy, which is a Dynamic Language with a powerful support of meta programming. Tha is basically the trick of all the magic of Grails

Finally, Grails is a framework based on CoC (Convection over configuration), So the Controllers will be any class under the directory "grails-app/controllers" and with the suffix "Controller". (In the folder of controllers may be "commandObjects as well).

The integration with well-know ides is quite powerful as well, you should check it out

EDIT You may also found how the render methods behaves here at github. And more inyected stuff at the Controllers metaClass package

Upvotes: 2

Related Questions