首先需要建立Struts2 HelloWorld,然后再使用eclipse的debug功能查看Struts2的源码。
当一个请求到来时会经过Struts2Filter过滤(调用其中的doFilter)。
①.Struts2的request处理流程如下:
下面我们来看看struts2的部分源码:
1.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);//调用ExecuteOperations的executeAction方法 } } } finally { prepare.cleanupRequest(request); } }
2.DefaultActionInvocation中的invoke方法
public String invoke() throws Exception { String profileKey = "invoke: "; try { UtilTimerStack.push(profileKey); if (executed) { throw new IllegalStateException("Action has already executed"); } if (interceptors.hasNext()) {//判断是不是还有下一个InterceptorMapping,每个InterceptorMapping里面存放着一个Interceptor final InterceptorMapping interceptor = interceptors.next();//获取下一个InterceprorMapping String interceptorMsg = "interceptor: " + interceptor.getName();//获取interceptor的名字 UtilTimerStack.push(interceptorMsg); try { //得到Interceptor实例,然后在调用该实例的intercept方法,DefaultActionInvocation自己作参数 resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); } finally { UtilTimerStack.pop(interceptorMsg); } } else {//如果interceptors中没有了IntercetorMapping,说明所有的interceptor都调用完了。 //当调用完所有的interceptor之后,再调用action,在里面使用了反射调用了action中的方法 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 (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()) { executeResult(); } executed = true; } return resultCode; } finally { UtilTimerStack.pop(profileKey); } }
3.随便找一个interceptor的,这个以ExceptionMappingInterceptor为例,查看其中的部分源码:
public String intercept(ActionInvocation invocation) throws Exception { String result; try { //调用DefaultActionInvocation的invoke方法 result = invocation.invoke(); } catch (Exception e) { if (isLogEnabled()) { handleLogging(e); } //获取struts.xml配置文件中配置的Exception Mapping,返回的是一个List List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings(); //在集合List中是否存在action抛出这个异常,如果存在,返回Exception Mapping 对应result String mappedResult = this.findResultFromExceptions(exceptionMappings, e); if (mappedResult != null) {//看result是否为null result = mappedResult; publishException(invocation, new ExceptionHolder(e));//把异常的信息放入Value Stack中 } else { throw e; } } return result; }
②.在DefaultActionInvocation中设置一个端点,在debug视图(显示方法执行流)查看详细的调用流程图
A.设置端点的位置如下:
B.查看debug视图:
从上的debug视图,我们可以看到详细的方法调用过程
相关推荐
STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析...
struts2源码解析.pdf
Struts1源码解析,有你看源码的步骤,你只要按照一步一步的来看,救没有问题了,struts就是一个菜
struts2源码详细解析51CTO下载-struts2源代码分析(个人觉得非常经典)
Strut2源码分析,写的非常好, 喜欢Struts2的人可以看一看
struts2源码解析[归纳].pdf
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品. 文档中对于代码进行重要部分...
下载的Struts2源代码文件是一个名叫struts-2.1.0-src.zip的压缩包,里面的目录和文件非常多,读者可以定位到struts-2.1.0-src"struts-2.0.10"src"core"src"main"java目录下查看Struts2的源文件
struts2深入详解 源码 完整 1-5章 含jar包
Struts2的源码库,以及对Struts2的源码解析文档,非常经典
struts源码解析,这是业余的时候结合网上高手的见识而总结的一套struts源码,分享给大家,希望有帮助,同类文件还有,Spring源码解析 .欢迎下载.
《Struts2技术内幕:深入解析Struts2架构设计与实现原理》由国内极为资深的Struts2技术专家(网名:downpour)亲自执笔,iteye兼CSDN产品总监范凯(网名:robbin)以及51CTO等技术社区鼎力推荐。 本书以Struts2的...
本资源包含两个学习Struts的资料: 1.struts2基础.chm 2.Struts2+技术内幕——深入解析Struts2架构设计与实现原理.pdf 欢迎有兴趣的童鞋下载学习。
Struts2_动态方法调用,使用struts2架构的动态方法进行资源调用
NULL 博文链接:https://chaoyi.iteye.com/blog/2156728
该文档是针对解析Struts源码所编写的一个文档,是入门Struts,精通Struts的精品文档,该文档为北大青鸟学习Struts的内部资料!
在核心实现篇中,通过源码解析,帮助广大开发者从Struts 2自身的设计原理上去掌握Web层开发的要点,如数据、动作、拦截器、视图、Plugin、配置、过程;最佳实践篇介绍了一些在实战中提炼出来的针对Struts 2的扩展和...
本Java视频教程对 Struts2 庞杂的技术点进行抽丝剥茧,提炼出企业开发必备的核心技术和重要技能。每个技术点都配备案例和代码,对于拦截器、Struts2 运行流程分析、值栈等技术点的讲授更是深入解析源代码,授之以渔
[Android访问Struts2服务器程序]我发现在网上下载这个源码很困难,我一步步配置成功了,还将具体应该注意的事项都列出来了,很详细,能够运行成功! Android+struts2+JSON形式的手机开发 把本机当作服务器,将Android...
Java中:struts2+jQuery+ajax调用(引用) 代码,解析,源码,demo,实例,分析