Result

Action方法被调用后,返回结果字符串,用它来匹配xml中定义的resultType的name属性,如果匹配上则使用该Result来处理结果:渲染页面、下载文件等等。

 

 public Result createResult() throws Exception {

        if (explicitResult != null) {
            Result ret = explicitResult;
            explicitResult = null;

            return ret;
        }
        ActionConfig config = proxy.getConfig();
        Map<String, ResultConfig> results = config.getResults();//获取该Action的结果配置

        ResultConfig resultConfig = null;

        try {
            resultConfig = results.get(resultCode);//根据返回值来匹配
        } catch (NullPointerException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Got NPE trying to read result configuration for resultCode [#0]", resultCode);
            }
        }
        
        if (resultConfig == null) {
            // If no result is found for the given resultCode, try to get a wildcard '*' match.
            resultConfig = results.get("*");
        }

        if (resultConfig != null) {
            try {
                return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());//实例化Result
            } catch (Exception e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("There was an exception while instantiating the result of type #0", e, resultConfig.getClassName());
                }
                throw new XWorkException(e, resultConfig);
            }
        } else if (resultCode != null && !Action.NONE.equals(resultCode) && unknownHandlerManager.hasUnknownHandlers()) {
            return unknownHandlerManager.handleUnknownResult(invocationContext, proxy.getActionName(), proxy.getConfig(), resultCode);
        }
        return null;
    }

  private void executeResult() throws Exception {
        result = createResult();

        String timerKey = "executeResult: " + getResultCode();
        try {
            UtilTimerStack.push(timerKey);
            if (result != null) {
                result.execute(this);//执行结果
            } else if (resultCode != null && !Action.NONE.equals(resultCode)) {
                throw new ConfigurationException("No result defined for action " + getAction().getClass().getName()
                        + " and result " + getResultCode(), proxy.getConfig());
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("No result returned for action " + getAction().getClass().getName() + " at " + proxy.getConfig().getLocation());
                }
            }
        } finally {
            UtilTimerStack.pop(timerKey);
        }
    }

 

 


Result_第1张图片
 

你可能感兴趣的:(result)