这个监听器的作用是在执行完成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;
}
}