Controller action implicitly called when rendering template without a model?

151 views
Skip to first unread message

Ted Vinke

unread,
Sep 3, 2014, 5:33:20 AM9/3/14
to grails-de...@googlegroups.com
Today I was confronted by some odd behaviour 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".

controllers/test/RenderController with explicit empty model
class RenderController {
 
    
def index() {
        render
template: "index", model: [:] // <-- explicit empty model
   
}
 
 
   
def getSomethingElse() {
        render
"something else"
   
}
}


views/render/_index.gsp
<h1>Hello from the _index.gsp</h1>

When http://localhost:8080/RenderTest/render is invoked, just the contents from the _index.gsp are shown:

Hello from the _index.gsp

As expected.
When I leave out the model attribute to the template rendering...

def index() {
    render
template: "index" // <-- no model
}


...suddenly the getSomethingElse() method is invoked too, just before that. When reloading the page, "something else" is rendered, followed by the contents on my index-snippet:

something else
Hello from the _index.gsp

What I did NOT expect is that basically every additional action - in this controller - starting with "get" seems to be invoked before my render, that's at least the pattern I can see here, if I do not explicitly define a model passed to the "render" method.

I went through the Grails documentation and Grails JIRA, but couldn't find any documentation about this feature, or ticket to a bug, nor any reference from the Grails docs to leveraging any existing Spring MVC feature.
Who can help me finding where this behavior is documented?

Kind regards, 
Ted

Graeme Rocher

unread,
Sep 3, 2014, 5:37:44 AM9/3/14
to grails-de...@googlegroups.com
This is an old feature that will be removed in Grails 3.0

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

Cheers
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/b2827773-7611-4ba4-a55b-e747b26adf81%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Graeme Rocher
Grails Project Lead
Pivotal

Ted Vinke

unread,
Sep 3, 2014, 5:42:48 AM9/3/14
to grails-de...@googlegroups.com
Thanks for the quick response, Greame!
Is this feature documented somewhere? Or maybe a JIRA issue about it's pending removal in Grails 3.0?

Cheers

Graeme Rocher

unread,
Sep 3, 2014, 5:57:19 AM9/3/14
to grails-de...@googlegroups.com
Reference to it can be found in older user guides. Example in 2.1.x

http://grails.org/doc/2.1.0/guide/single.html#controllers

See "Returning the model" and the line:

"If no explicit model is returned the controller's properties will be
used as the model"

This was removed from the user guide however since the behaviour is
regarded as deprecated as we didn't want to encourage its use.

Cheers
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/fd295f8a-727a-43cf-8eb8-d0f6178ab1e5%40googlegroups.com.

Paco Zarate

unread,
Sep 4, 2014, 12:00:55 PM9/4/14
to grails-de...@googlegroups.com
This behavior caused me problems too, what is the right way to proceed on grails 2.x? Do not use the 'get' prefix on the method names?






Ted Vinke

unread,
Sep 13, 2014, 5:24:09 AM9/13/14
to grails-de...@googlegroups.com
Ha Paco,
It seems that whatever you do depends 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 I previously had none defined, and which triggered this behaviour - worked for me. Just one occurrence needed replacing.

In all cases I had to communicate this with the team, to be aware of this feature :-)
Regards, Ted

--
You received this message because you are subscribed to a topic in the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grails-dev-discuss/GDKW7_bK-NM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grails-dev-disc...@googlegroups.com.

To post to this group, send email to grails-de...@googlegroups.com.

Paco Zarate

unread,
Sep 22, 2014, 11:33:59 AM9/22/14
to grails-de...@googlegroups.com
Hey Ted! Thanks for your suggestions.

I will try the empty model approach. I have a lot of get methods on my REST API and I have the map pointing to the renamed method but it seems more natural if i can keep the names with the get.

Thanks again
Paco



Reply all
Reply to author
Forward
0 new messages