Struts拦截器使用
创建拦截器java程序
package cn.itcast.oa.util; 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, 在package里写拦截器内容
<package name="default" namespace="/" extends="struts-default"> <interceptors> <!-- 声明拦截器 --> <interceptor name="checkPrivilege" class="cn.itcast.oa.util.CheckPrivilegeInterceptor"></interceptor> <!-- 重写定义默认的拦截器栈 --> <interceptor-stack name="defaultStack"> <interceptor-ref name="checkPrivilege"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors>