Is there a way of executing some piece of code before any controller action gets called?
I need to set a session variable based on the value of a get parameter, without taking into account which controller gets called.
Of course, once this processing is done, the request needs to follow its normal way to the corresponding controller/action.
Thanks
Sounds like you want to use a filter.
e.g. grails-app/conf/MyFilter.groovy
class MyFilter {
def filters = {
extractSomething(controller: '*', action: '*') {
before = {
session.setAttribute('foo', params['paramName'])
}
}
}
}
filters are good if used with multiple or all controllers but could get expensive. you may also try interceptors:
def beforeInterceptor = {
session.setAttribute('foo', params['paramName'])
}
来源:https://stackoverflow.com/questions/5421815/grails-cross-controller-code-execute-on-every-request