Description

The beforeInterceptor intercepts processing before the action is executed. If it returns false then the action will not be executed. The interceptor can be defined for all actions in a controller as follows:

def beforeInterceptor = {
println "Tracing action ${actionUri}"
}

This declaration must be a class-scope Closure in the controller class. It will execute before all actions and does not interfere with processing since it returns no value. A common use case however is for simple authentication (although a security plugin is preferred):

This defines a method called auth. It is declared as a private method so it is not exposed as a controller action. The beforeInterceptor then defines an interceptor that is used on all actions except the login action and is configured using Groovy's method pointer syntax to execute the auth method. The auth method will redirect to another page if it doesn't find a user in the session, and it signifies that it has handled the response and that the requested action should not be processed by returning false.

This example limited the intercepted actions using the except argument but the only argument can also be used to list the actions to be intercepted, rather than those to not intercept.

Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
Sponsored by SpringSource