java学习笔记 (5) —— Struts2 监听器配置

1、创建MyListener.java 实现 PreResultLisener 接口

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener; public class MyListener implements PreResultListener { public void beforeResult(ActionInvocation invocation, String resultCode) {
System.out.println("result : "+resultCode);
} }

2、在拦截器中引入监听器

package com.test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.test.Listener.MyListener; public class MyInterceptorMet extends MethodFilterInterceptor { private static final long serialVersionUID = 8722841247439200544L; @Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener(new MyListener());
System.out.println("my interceptor method");
     // 执行 Action
String result = invocation.invoke();
return result;
}
}

3、执行顺序

validateDM is invoked —— 先进检验方法
my interceptor method —— 实例化监听器,并没有输出内容
DesignMethod is invoked —— 依然会执行Action方法
result : success —— 如果提交成功返回 success ,否则 返回input

上一篇:mybatis系列笔记(2)---mapper代理方法


下一篇:Java精选笔记_JavaBean