第一步:
创建自定义拦截器,实现AbstractInterceptor接口,或者直接实现Interceptor接口。
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class CheckPrivilegeInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("------拦截之前");
//添加业务逻辑
String result = invocation.invoke();// 放行
System.out.println("------拦截之后");
return result;
}
}
第二步:
配置struts.xml文件。
<interceptors>
<!--声明定义的拦截器-->
<interceptor name="checkInterceptor" class="cn.itcast.oa.interceptor.CheckPrivilegeInterceptor"></interceptor>
<interceptor-stack name="defaultStack"><!--把自定义拦截器栈重新命名为defaultStack-->
<interceptor-ref name="checkInterceptor"/><!--引用定义的拦截器-->
<interceptor-ref name="defaultStack"/><!--引用struts2默认的拦截器栈-->
</interceptor-stack>
</interceptors>