StrutsPrepareAndExecuteFilter:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
//如果访问不是action就放行
if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
prepare.setEncodingAndLocale(request, response);
prepare.createActionContext(request, response);//ActionContext(数据中心):数据中心的生命周期为(一旦请求就init()..响应完就destroy())
prepare.assignDispatcherToThread();
request = prepare.wrapRequest(request);//wrap(包装模式):改变原有类的某些功能
//ActionMapping(接待员):mapping数据集为map{namespace..action_name..method..前三个更为重要(extension..params(requestParams))}
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);//F3
}
}
} finally {
prepare.cleanupRequest(request);
}
}
ExecuteOperations:
public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
dispatcher.serviceAction(request, response, mapping);//F3
}
Dispatcher:
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
throws ServletException {
Map<String, Object> extraContext = createContextMap(request, response, mapping);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
...
String timerKey = "Handling request from Dispatcher";
try {
UtilTimerStack.push(timerKey);
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
//ConfigurationManager(配置管家)中加载过的信息以及与ActionMapping中的报告对象产生ActionProxy proxy对象
ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
//StrutsActionProxy proxy(经理Impl)(继承DefaultActionProxy..DefaultActionProxy实现ActionProxy)..中执行DefaultActionInvocation(秘书Impl)中invoke()方法..F3
proxy.execute();
}
// If there was a previous value stack then set it back onto the request
...
}
StrutsActionProxy:
public String execute() throws Exception {
...
return invocation.invoke();//DefaultActionInvocation invocation类执行invoke().F3
}
DefaultActionInvocation:
public String invoke() throws Exception {
...
//递归主代码:
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = interceptors.next();
String interceptorMsg = "interceptor: " + interceptor.getName();
UtilTimerStack.push(interceptorMsg);
try {
--将invocation传入
//选中interceptor.getInterceptor()右键inspect查看当前是哪个Intercept实现类..接着查看这个实现类intercept
发现其中一行代码为 invocation.invoke()..经过20个拦截器
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
}
finally {
UtilTimerStack.pop(interceptorMsg);
} else {
resultCode = invokeActionOnly();//此时执行<action中的method方法运行..返回String resultCode
}
//executeResult()
if (!executed) {//如何之前没有处理结果<result 就执行一下代码..
if (preResultListeners != null) {
for (Object preResultListener : preResultListeners) {
PreResultListener listener = (PreResultListener) preResultListener;
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()) {
//执行方式通过查找struts.xml中<result 中的type属性对应的类..执行类中doExecute()
(具体哪个类:可以参考struts-core包下的struts-default.xml中的result-types标签)
executeResult();
}
executed = true;
}
最后:
执行Intercept实现类中的后处理..将结果响应回客户端