public abstract void destroy(); public abstract void init(); public abstract String intercept(ActionInvocation paramActionInvocation) throws Exception;
public class TestAction extends ActionSupport {
public String execute(){
System.out.println("执行execute方法......");
return SUCCESS;
}
}
public class MyIntercept implements Interceptor {
public void init() {}
public void destroy() {}
public String intercept(ActionInvocation action) throws Exception{
System.out.println("拦截action开始.......");
String result = action.invoke();
System.out.println("拦截action结束.......");
return result;
}
}
<interceptors> <interceptor name="test" class="MyPackage.TestAction"/> ...... ...... </interceptors>
<action name="test" class="MyPackage.TestAction"> <interceptor-ref name="test"/> <result name="success">/index.jsp</result> ...... ...... </action>
<interceptor-stack name="拦截器栈名"> interceptor-ref name="拦截器一"/> interceptor-ref name="拦截器二"/> interceptor-ref name="拦截器三"/> ..... </interceptor-stack>
interceptor-ref name="拦截器栈名"/>
<default-interceptor-ref name="拦截器名"/>
public abstract class MethodFilterInterceptor
extends AbstractInterceptor
{
//该set集合保存了该拦截器不需要拦截的所有方法
protected Set<String> excludeMethods = Collections.emptySet();
//该set集合保存了所有该拦截器需要拦截的方法
protected Set<String> includeMethods = Collections.emptySet();
//省略getter,setter方法
//用于拦截action的入口
public String intercept(ActionInvocation invocation)
throws Exception
{
if (applyInterceptor(invocation)) {
return doIntercept(invocation);
}
return invocation.invoke();
}
//判断当前需要调用的action处理逻辑方法是否需要被此拦截器拦截
protected boolean applyInterceptor(ActionInvocation invocation)
{
String method = invocation.getProxy().getMethod();
boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(this.excludeMethods, this.includeMethods, method);
if ((this.log.isDebugEnabled()) &&
(!applyMethod)) {
this.log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.", new String[0]);
}
return applyMethod;
}
//这是需要我们重写的方法,具体作用下文介绍
protected abstract String doIntercept(ActionInvocation paramActionInvocation)
throws Exception;
}
//自定义一个拦截器
public class MyIntercept extends MethodFilterInterceptor {
protected String doIntercept(ActionInvocation action)
throws Exception{
System.out.println("拦截开始......");
String result = action.invoke();
System.out.println("拦截结束......");
return result;
}
}
//引用该拦截器并指定不需要拦截的方法
<action name="test" class="MyPackage.TestAction">
<interceptor-ref name="test">
<param name="excludeMethods">execute</param>
</interceptor-ref>
<result name="success">/index.jsp</result>
</action>
<action name="test" class="MyPackage.TestAction">
<interceptor-ref name="test">
<param name="includeMethods">execute</param>
</interceptor-ref>
<result name="success">/index.jsp</result>
</action>
<param name="includeMethods">方法一,方法二,方法三</param>
<action name="test" class="MyPackage.TestAction"> <interceptor-ref name="test"/> <interceptor-ref name="test2"/> <result name="success">/index.jsp</result> </action>
<interceptor-ref name="拦截器栈"> <param name="拦截器一.属性名">属性值</param> </interceptor-ref>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有