Struts2源码分析讨论

本文探讨了Struts2框架中DefaultActionInvocation类的invoke方法实现细节,特别是如何迭代执行Interceptor中的intercept方法,揭示了拦截器的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近一直在研究struts2,看了李刚写的一本struts2权威指南,但感觉讲的还是有点浅,但是做为入门的话,还是不错的.现在开始看struts2的源码,但由于本人能力有限,看的是头晕脑涨的,有很多不明白的地方,所以想把不明白的地方贴出来跟大家一起讨论讨论.
其中有一处也觉的是比较关键的一步:
以下是DefaultActionInvocation中的invoke()方法的源码:

[code]
public String invoke() throws Exception {
String profileKey = "invoke: ";
try {
UtilTimerStack.push(profileKey);

if (executed) {
throw new IllegalStateException("Action has already executed");
}

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 {
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
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
if (proxy.getExecuteResult()) {
executeResult();
}

executed = true;
}

return resultCode;
}
finally {
UtilTimerStack.pop(profileKey);
}
}[/code]
不明白是怎样迭代执行一个又一个的Interceptor中的intercept方法.
按照我的理解,至少我可以看到一个循环遍厉interceptors这个集合才对.
大家可以讨论讨论,有知道的也可以告诉我.谢谢.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值