OptionalbeforeInterceptor
Attribute can interrupt the operation of an action before it starts to run.
Simple tracking Interceptor:
def beforeInterceptor = { println "Tracing action ${actionUri}"}
A security verification interceptor ensures that the user has logged on:
def beforeInterceptor = [action:this.&auth,except:'login']// defined as a regular method so its privatedef auth() { if(!session.user) { redirect(action:'login') return false }}def login = { // display login page}
beforeInterceptor
The interceptor can interrupt its operation before the action starts. If the interceptor returnsfalse
The action will not be executed. You can add the Interceptor to all actions in the Controller as follows:
def beforeInterceptor = { println "Tracing action ${actionUri}"}
The preceding statement needs to be placed in the Controller definition. It will be executed before all actions are run, but will not affect the action processing process. A common usage is authentication:
def beforeInterceptor = [action:this.&auth,except:'login']// defined as a regular method so its privatedef auth() { if(!session.user) { redirect(action:'login') return false }}def login = { // display login page}
The above Code sets a method called 'auth ', a method that will not be exposed to Action (that is, it is private ). The 'beforeinterceptor' attribute defines an interceptor for all actions (except the login action). The interceptor will execute the 'auth' method with the groovy method pointer syntax. In this method, first determine whether the current user has set a session. If not, it will redirect to the login action and return false, other code of the interrupted action will not be executed.