Struts2源码分析讨论

[复制链接]
查看11 | 回复3 | 2008-1-31 11:12:36 | 显示全部楼层 |阅读模式
最近一直在研究struts2,看了李刚写的一本struts2权威指南,但感觉讲的还是有点浅,但是做为入门的话,还是不错的.现在开始看struts2的源码,但由于本人能力有限,看的是头晕脑涨的,有很多不明白的地方,所以想把不明白的地方贴出来跟大家一起讨论讨论.
其中有一处也觉的是比较关键的一步:
以下是DefaultActionInvocation中的invoke()方法的源码:
Java代码
public String invoke() throws Exception {
String profileKey = "invoke: ";
try {

UtilTimerStack.push(profileKey);



if (executed) {

throw new IllegalStateException("Action has already executed&quot

;

}

if (interceptors.hasNext()) {

final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();

UtilTimerStack.profile("interceptor: "+interceptor.getName(),

new UtilTimerStack.ProfilingBlock() {

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);
}
}
public String invoke() throws Exception {

String profileKey = "invoke: ";

try {

UtilTimerStack.push(profileKey);



if (executed) {

throw new IllegalStateException("Action has already executed&quot

;

}

if (interceptors.hasNext()) {

final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();

UtilTimerStack.profile("interceptor: "+interceptor.getName(),

new UtilTimerStack.ProfilingBlock() {

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);

}
}
不明白是怎样迭代执行一个又一个的Interceptor中的intercept方法.
按照我的理解,至少我可以看到一个循环遍厉interceptors这个集合才对.
大家可以讨论讨论,有知道的也可以告诉我.谢谢.
回复

使用道具 举报

千问 | 2008-1-31 11:12:36 | 显示全部楼层
这个嘛,你在找个Interceptor看看,你会发现它调用了这个invoke(),这个好像叫递归吧,说错了当没说啊(偶水平也很差啊)
回复

使用道具 举报

千问 | 2008-1-31 11:12:36 | 显示全部楼层
恩,果然是这样.终于搞清楚了整个流程.较struts1而言,struts2的设计真是巧妙加精华.如果大家有什么关于struts2源码的问题,也可以发出来大家讨论.希望可以给大家帮助.
回复

使用道具 举报

千问 | 2008-1-31 11:12:36 | 显示全部楼层
你好,想请教下,怎么在自定义的拦截器中得请求参数的值呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行