Struts2源码图解

1.jpg

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实现类中的后处理..将结果响应回客户端

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Action 1.1 请求相应模式 请求-响应模式是一种概念非常宽泛的人机交互模式,是人与计算机进行沟通的一...
    fredal阅读 4,030评论 0 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,461评论 19 139
  • 为什么要设计sturts? Struts设计的第一目标就是使MVC模式应用于web程序设计。技术优势 Struts...
    FTOLsXD阅读 2,682评论 1 1
  • 这里我们只是讲一下最基本的原理,也就是当页面请求提交过来之后struts2是怎样处理的。在网上找了一张图,地址是:...
    yjaal阅读 3,372评论 0 3
  • 原创版权申明:本文章从本人 csdn 博客转到简书。如有转载,请申明:转载自 IT天宇:http://www.ji...
    IT天宇阅读 4,298评论 0 15