前面稍微学习了下Strust2基本使用,对Struts2的工作流程以及底层源码完全不懂,今天打算把Struts2的工作流程好好的摸索一遍。
1.这是一张网上download的struts2工作流程图,
对上图稍做解释:
1.首先客户端/浏览器发送一个请求到服务器,即HttpServletRequest会经过一系列(Fliter)过滤器(ActionContextCleanUp该过滤器是可选过滤器,主要作用就是对ActionContext进行CleanUp操作,不让后续的Fliter清除,延长action中属性的生命周期,以便在jsp中访问。)
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
prepare.setEncodingAndLocale(request, response);//对locale、encoding进行设置
prepare.createActionContext(request, response);//创建AcionContext,即action上下文
prepare.assignDispatcherToThread();
request = prepare.wrapRequest(request);//对request进行包装
ActionMapping mapping = prepare.findActionMapping(request, response, true);//得到action mapping
//如果mapping为空,则不会调用action,会调用下一个过滤器链,直到获取到mapping才调用action
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
//不为空时,则执行action
} else {
execute.executeAction(request, response, mapping);
}
}
} finally {
prepare.cleanupRequest(request);
}
}
public void destroy() {
prepare.