Struts2拦截器基础知识

来源:互联网 发布:c语言层次遍历二叉树 编辑:程序博客网 时间:2024/06/05 16:26

拦截器API

Interceptor
为Struts2框架中定义的拦截器对象,接口,Struts2内置拦截器对象或者自定义拦截器都需要直接或间接实现此接口。

public interface Interceptor extends Serializable{    void destroy();    void init();    String intercept(ActionInvocation invocation)throws Exception;}
  • destroy():在拦截器被销毁前调用,用于释放初始化时占用的资源。
  • init():初始化,在拦截器被实例化,intercept()方法执行前调用。
  • intercept():主要方法,用于执行Action对象中的请求处理方法以及在Action的前后进行操作,动态增强Action的功能。只有调用invocation参数的incoke(),才可以执行Action对象中的请求处理方法。

AbstractInterceptor

创建拦截器可以通过继承AbstractInterceptor来创建,重写必需的intercept(),如果没有用到init()和destroy(),可以不实现。
AbstractInterceptor已经对Interceptor的init()和destroy()进行了实现。

创建拦截器

  1. 继承AbstractInterceptor
  2. 使用标签进行配置
原创粉丝点击