1.主要功能&触发时间
该监听器主要在action的execute方法执行完以后,result.execute()方法执行前调用。
接口如下
在DefaultActionInvocation对应的调用如下
2.注册方法
通过调用 invocation.addPreResultListener具体代码如下(摘自struts2 docs)
该监听器主要在action的execute方法执行完以后,result.execute()方法执行前调用。
接口如下
public interface PreResultListener {
void beforeResult(ActionInvocation invocation, String resultCode);
}
在DefaultActionInvocation对应的调用如下
//判断是否还有拦截器未执行,如果还有则继续执行拦截器链
//这里通过把DefaultActionInvocation对象本身往后续拦截器中传递来实现interceptors这个interceptor的迭代。
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
UtilTimerStack.profile("interceptor: "+interceptor.getName(),
new UtilTimerStack.ProfilingBlock<String>() {
public String doProfiling() throws Exception {
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
return null;
}
});
} else {
//所有拦截器执行完成调用action的执行方法。
resultCode = invokeActionOnly();
}
// this is needed because the result will be executed, then control will return to the Interceptor, which will
// return above and flow through again
//调用preResultListeners的内容
if (!executed) {
if (preResultListeners != null) {
for (Iterator iterator = preResultListeners.iterator();
iterator.hasNext();) {
PreResultListener listener = (PreResultListener) iterator.next();
String _profileKey="preResultListener: ";
try {
UtilTimerStack.push(_profileKey);
listener.beforeResult(this, resultCode);
}
finally {
UtilTimerStack.pop(_profileKey);
}
}
}
// now execute the result, if we're supposed to
//调用result.execute方法.
if (proxy.getExecuteResult()) {
executeResult();
}
executed = true;
}
2.注册方法
通过调用 invocation.addPreResultListener具体代码如下(摘自struts2 docs)
public class MyAction extends ActionSupport {
...
public String execute() throws Exception {
ActionInvocation invocation = ActionContext.getActionInvocation();
invocation.addPreResultListener(new PreResultListener() {
public void beforeResult(ActionInvocation invocation,
String resultCode) {
// perform operation necessary before Result execution
}
});
}
...
}