Last active
July 7, 2016 08:19
-
-
Save baybatu/4676d37dfd27d519c84dd345843a88c1 to your computer and use it in GitHub Desktop.
Using safe method names in 'beforeInterceptor' interceptor in Grails 2.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyController { | |
/** | |
* Normally, String type method name in except part of beforeInterceptor is legal. For example: | |
* | |
* def beforeInterceptor = [action: this.&intercept, except: ['index', 'hede', 'hodo']] | |
* | |
* But this is not safe for future naming refactorings. Instead, using closure-maker | |
* ampersand operator(.&) before method name and then getting 'method' property to get | |
* method name as String is better for future safe changes. | |
*/ | |
def beforeInterceptor = [action: this.&intercept, except: [this.&index.getProperty('method'), | |
this.&hede.getProperty('method'), | |
this.&hodo.getProperty('method')]] | |
def index() { | |
render "Exceptional endpoint, I am in index." | |
} | |
def hede() { | |
render "Exceptional endpoint, I am in hede." | |
} | |
def hodo() { | |
render "Exceptional endpoint, I am in hodo." | |
} | |
def helehele() { | |
render "Enters intercept method before coming here. Hele Hele Hele is here!.." | |
} | |
private boolean intercept() { | |
// Enters here before request handler method invocation and returns boolean true | |
// if the method is wanted to be called properly. Otherwise returns false. | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment