读书人

Struts2的监听器的施用

发布时间: 2012-10-27 10:42:25 作者: rapoo

Struts2的监听器的使用
一,建立监听器:
实现com.opensymphony.xwork2.interceptor.PreResultListener接口
public class MyListener implements PreResultListener {

public void beforeResult(ActionInvocation actionInvocation, String resultCode) {

System.out.println("监听器:"+resultCode);

}

}

二,注册监听器(要在拦截器中注册临听器)
public class MyInterceptor extends AbstractInterceptor {

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

/**
* 注册监听器
*/
actionInvocation.addPreResultListener(new MyListener());

System.out.println("拦截器之前");
String result = actionInvocation.invoke();
System.out.println("拦截器之后");
return result;
}

}

三,监听器是在Action里的方法执行完后,在出拦截器方法之前执行!

读书人网 >软件架构设计

热点推荐