Reputation: 11
my enviroment:
java version "1.6.0_38"
Grails 2.1.1
grails create-app test
cd test<br>
grails create-controller com.test.Test
There is NO SPECIAL CONFIG in UrlMapping
.
com.test.TestController
:
package com.test
class TestController {
def getA(){
def ret = []
println "in getA"
render ret
}
def index() {
println "in index"
render view:"index"
}
}
grails-app/test/index.gsp:
<html>
<body> hello </body>
</html>
when i try to access [ http://HOSTNAME/PRJNAME/test/index
]
i suppose that would happend:
HTML:
<html>
<body>
hello
</body>
</html>
Console :
in index
BUT,things not like that, actually, it's like this:
HTML:
[]<html>
<body>
hello
</body>
</html>
Console :
in index
in getA
Why??
Upvotes: 1
Views: 153
Reputation: 75671
Grails calls all methods that start with "get" to find closures that are used as actions. Since the syntax def foo = { ... }
defines a closure as a property, Groovy adds a getter and setter (getFoo
and setFoo
), so the closures are discovered by finding properties and calling the getters to check if the return type is a Closure. Since controllers are (by default) prototype scoped they're created for each request, each public getter method gets called for each request.
So the workaround is to not start action methods with "get".
Upvotes: 1