grails3版本的拦截器

grails3版本中,去掉了Filter,拦截器功能由Interceptor完成。

Interceptor在controller所在的目录下定义,命名规则是:名称+Interceptor。

内部需要实现三个方法:boolean before()【执行方法之前拦截,默认返回true】、boolean after()【执行方法之后拦截,默认返回true】、void afterview()【页面渲染】;当before()方法返回为true时才会执行action方法,否则action方法以及after()、afterview()因为拦截而都不会被执行;当before()方法返回true,action方法正常执行后,会执行after()方法,当after()方法返回true时才会执行afterview()中的内容;当before()和after()方法内容为空时,执行完action后直接执行afterView()方法。

  • 使用1:

命名:controller名称+Interceptor;只拦截对应名称的controller类中的方法。如NumberOneInterceptor只拦截NumberOnecontroller中的所有方法

  • 使用2:

命名:任意名称+Interceptor;在该拦截器的无参构造中使用matchAll()或match 方法来指定拦截的controller以及action;如:

表示:拦截所有controller中的任何action, 但是除名为login、findObjectForAjax、taskExecuteSituationForAjax的三个action

 

表示:只拦截NumberTwoController中的index方法;

注意:controller指定的是具体的controller名称,但是不带有“Controller”后缀,且首字母要小写,否则无效

猜你喜欢

转载自blog.csdn.net/hxr_lzy/article/details/81407509
今日推荐