您的位置:首页 > 其它

Action的监听器——PreResultListener

2011-11-20 23:17 302 查看
这个监听器的作用是在执行完成Action后,在转发到其他Action或是显示结果之前,会调用这个类中的beforeResult()方法,以实现一些功能。通过invocation的addPreResultListener方法对监听器进行注册。

示例:

监听器类:

package com.suo.listeners;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class BeforeResultListener implements PreResultListener {

@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
// TODO Auto-generated method stub
System.out.println("beforeResult invoke ! "+resultCode);
}

}
拦截器:

package com.suo.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

@Override
public void destroy() {
// TODO Auto-generated method stub
}

@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("init invoke !");
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {

invocation.addPreResultListener(new BeforeResultListener());//增加一个监听器

System.out.println("before MyInterceptor invoke !");

String result=invocation.invoke();

System.out.println("after MyInterceptor invoke !");

return result;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息