It took a while to figure it out, which finally got cleared up with Graeme Rocher, but I’m posting here my original question and answers from the Grails Dev Discussion Google group for future reference.

I was confronted by some odd behavior in my Grails 2.3.6 application: when rendering a template in a controller action without specifying a model other controller action(s) is/are invoked too automatically. A small contrived example shows what I mean. A controller with an index() action – for rendering an “_index.gsp” snippet – and another action getSomethingElse() which renders “something else”.

class RenderController {
 
    def index() { 
        render template: "index" // <-- no model
    }
 
 
    def getSomethingElse() {
        render "something else"
    }
}

By not passing a model, implicitly getSomethingElse is called. I completely forgot why this happened.

After asking a question in the Google group, Graeme responded:

If no model is specified the controllers properties become the model. Because a getter is a property your action is invoked. This behaviour has indeed caused much confusion amongst users and hence why it will be removed in Grails 3.0

In older versions  – e.g. 2.1.x – it was described under chapter 7.1.3 “Returning the model” as

“If no explicit model is returned the controller’s properties will be used as the model”

To discourage its use it has already been removed from the documentation, but the behavior is still present until Grails 3 comes out.

As questioned in the discussion about how to proceed with Grails 2.x applications in this situation where you don’t have a model and one or more “getters”, it seems there are simply two options depending on your application.

  1. Renaming your ‘getXXX” into ‘findXXX’ might be an option – e.g. if you have a few occurrences and you don’t mind this renaming
  2. Adding an explicit empty model attribute (“model: [:]“) – where you previously had none defined
Advertisements