execAndWait拦截器

来源:互联网 发布:数码宝贝tri5 知乎 编辑:程序博客网 时间:2024/04/30 05:09

由于某个Action要执行较长的时间,所以在切换到下一个有效页面之前。一般先会进入一个等待的页面,等待页面装载。在互联网上这种页面的等待的效果会经常被用到,现在就借用Struts2的拦截器来实现这个功能。 使用到的拦截器为execAndWait拦截器。分析拦截器的源码。

public class ExecuteAndWaitInterceptor extends MethodFilterInterceptor {   public static final String WAIT = "wait";
 protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
/*------*/

return WAIT;

}

}

上面的代码中没有看到Interceptor()方法的实现。所以到父类中看个究竟。

public abstract class MethodFilterInterceptor extends AbstractInterceptor {

 @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        if (applyInterceptor(invocation)) {
            return doIntercept(invocation);
        } 
        return invocation.invoke();
    }

 protected abstract String doIntercept(ActionInvocation invocation) throws Exception;

}

所以只要实现 doIntercept()方法。拦截器执行后会返回WAIT.

execAndWait拦截器的执行过程。execAndWait拦截器能够让一个执行时间较长的Action在后台执行,是通过生成一个单独的线程的形式,单独执行这个时间较长的Action。在Action执行结束之前将放回WAIT结果,WAIT结果同时负责让后续的请求返回到这个Action。在Action结束后返回Action中execute方法返回的字符串,程序继续执行。


所以这样可以实现在一个长时间Action没有完成时,我们可以让页面挺留在一个WAIT页面。

<result name="wait" type="dispatcher">

<param name="location">wait.html</param>

</result>


具体例子见E:\javaEE\Interceptor

原创粉丝点击