动态代理生成action这部分源码,应该算是Struts2之中比较底层的代码了,在这里贴出源码的执行流程,仅供参考。
核心过滤器StrutsPrepareAndExecuteFilter,在此只贴出doFilter()的代码
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
prepare.setEncodingAndLocale(request, response);
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
request = prepare.wrapRequest(request);
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);
}
}
} finally {
prepare.cleanupRequest(request);
}
}
进入execute.executeAction(request, response, mapping);
进入ExecuteOperations的serviceAction()
public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
dispatcher.serviceAction(request, response, servletContext, mapping);
}
进入Dispatcher类之中的serviceAction()方法
点进去,进入DefaultActionProxyFactory类的createActionProxy()方法
进入这个类的重载方法
进入DefaultActionProxy的prepare()方法
进入DefaultActionInvocation的init()方法
进入DefaultActionInvocation的createAction()方法
进入ObjectFactory的buildAction()方法
跳转
点击buildBean()方法
会发现这里运用反射创建代理action的instance,并返回,完成action的创建。