Grails provides filters that run before your controllers. They're defined in classes that look like this:
class SecurityFilters {
def filters = {
myFilter(controller:'*', action:'*') { // What are those weird colons??
print "I'm filtering!"
// Code that does the filtering goes here
}
}
}
These work great but I would like to understand the syntax better as it doesn't look like any Groovy code I've seen before. In particular, the line above that starts with myFilter
seems very odd. Is this a method definition for a method called myFilter
? If so, what does :'*'
mean after each parameter? I thought it might be a default parameter value but that would be ='*'
. I've seen named parameters using colons in method calls before but this couldn't be a method call because I haven't defined myFilter()
anywhere else.
I think I'd understand much better if someone could just tell me how to execute the filtering code from a normal Groovy class. In other words, if I have a file MyFilters.groovy that contains the lines above, how could I finish this Groovy code so it prints "I'm filtering"?
import MyFilters
def mf = new MyFilters()
mf.filters.somethingGoesHere // Help! How do I finish this line so it calls my filtering code?