Search code examples
grailsgrails-controller

Grails - cross controller code, execute on every request


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


Solution

  • 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'])
                }
            }
        }
    }