美文网首页
05-SpringMVC请求过程分析(二)

05-SpringMVC请求过程分析(二)

作者: AcientFish | 来源:发表于2020-02-13 12:50 被阅读0次

    首先更正一个错误,在上节中我们讲过doMatch中的matchStrings方法会在后面在handlerAdapter处理时还会调用一次这个方法,在这个方法里面会解析url参数并将其绑定在request中。实际上这个操作是在getHandler的时候进行的,在得到最佳匹配mapping之后会执行handleMatch(bestMatch.mapping, lookupPath, request)方法将mapping和request绑定。这里RequestMappingInfoHandlerMapping重写了handleMath方法。我们一起来看看

    protected void handleMatch(RequestMappingInfo info, String lookupPath, HttpServletRequest request) {
            // 首先还是调用父类方法绑定mapping
            super.handleMatch(info, lookupPath, request);
            String bestPattern;
            Map<String, String> uriVariables;
            Set<String> patterns = info.getPatternsCondition().getPatterns();
            if (patterns.isEmpty()) {
                bestPattern = lookupPath;
                uriVariables = Collections.emptyMap();
            } else {
                // 这里会根据pattern重新获取url中的参数
                bestPattern = patterns.iterator().next();
                uriVariables = getPathMatcher().extractUriTemplateVariables(bestPattern, lookupPath);
            }
            request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, bestPattern);
            // 将得到的参数decode,以免前台传参encode导致乱码        
            Map<String, String> decodedUriVariables = getUrlPathHelper().decodePathVariables(request, uriVariables);
            // 注意。这里就将参数绑定在了request中,后面Adapter调用时直接从这里取,取不到会报错
            request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, decodedUriVariables);
    }
    
    public boolean matchStrings(String str, @Nullable Map<String, String> uriTemplateVariables) {
        Matcher matcher = this.pattern.matcher(str);
        if (matcher.matches()) {
            if (uriTemplateVariables != null) {
                // 这里会循环获取@RequestMapping中配置的参数名,并从url中获取对应的值,然后put进map中
                for (int i = 1; i <= matcher.groupCount(); i++) {
                    String name = this.variableNames.get(i - 1);
                    String value = matcher.group(i);
                    uriTemplateVariables.put(name, value);
                }
            }
            return true;
        } else {
            return false;
        }
    }
    

    由于有朋友反馈流程图形式可能比代码形式更有利于阅读和理解,因此本文将首次尝试以时序图的形式来分析以简短篇幅。希望大家有什么意见或建议可以积极的和我反馈。我也会积极修改展现方式以达到更好的传播方式。


    SpringMVC执行过程.png

    至此SpringMVC的简单流程也就结束了,其他相对复杂的数据结构(例如JSON、数组及文件上传等)暂时没有分析。但是具体过程大同小异,后面有机会的话我们在逐一分析。另外考虑到现在越来越多的公司使用前后端分离,因此对于视图解析这部分也没有做详细分析,希望大家见谅。

    相关文章

      网友评论

          本文标题:05-SpringMVC请求过程分析(二)

          本文链接:https://www.haomeiwen.com/subject/dqvkfhtx.html