您的位置:首页 > 编程语言 > Java开发

struts2系列学习笔记(11)---------------拦截器

2011-01-21 11:30 459 查看
拦截器本质上就是过滤器,是基于AOP思想的产物.在Struts2中,拦截器的作用十分重要!因为框架中大部分的核心功能都是通过拦截器实现的.这样的方式对于系统解耦十分重要.(但是对它的性能也十分担心.....)

1.拦截器的编写:

1)实现com.opensymphony.xwork2.interceptor.Interceptor接口

该接口定义得方法: void init()

void destroy()

String intercept(ActionInvocation invocation)throws Exception

2)继承抽象类com.opensymphony.xwork2.interceptor.AbstractInterceptor

该抽象类是接口的子类,该类对init和destroy方法做了空实现

3)struts2提供的基类拦截器:com.opensymphony.xwork2.interceptor.MethodFilterInteceptor

该类可以针对特定方法进行拦截,反过来也可以排除特定方法

2. 拦截器的定义

1)<interceptor name="拦截器名" class=“拦截器实现类"/>



<interceptor name="拦截器名" class=“拦截器实现类">

<param name="参数名">参数值</param> 注:指定参数

</interceptor>

2)

<interceptor-stack name="拦截器名">

<interceptor-ref name="拦截器一"/>

<interceptor-ref name="拦截器二"/> 注:可以引用其它拦截器栈

.....

</interceptor-stack>

3)默认拦截器:每个包只能指定一个默认拦截器,该包中未指定拦截器的Action,默认拦截器将起作用

<default-interceptor-ref name="拦截器或者拦截器栈名"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: