3

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

4

2 に答える 2

4

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'])
            }
        }
    }
}
于 2011-03-24T15:47:41.193 に答える
2

フィルタは、複数またはすべてのコントローラで使用する場合に適していますが、高価になる可能性があります。インターセプターを試すこともできます。

def beforeInterceptor = {
       session.setAttribute('foo', params['paramName'])
}

http://www.grails.org/Controllers+-+Interceptors

于 2011-03-24T18:24:58.497 に答える