Webx Json输出源码

Pipeline.xml

                 <when>
                    <!-- 创建JSON,无模板,无layout。 -->
                    <pl-conditions:target-extension-condition extension="json" />
                    <pl-valves:performScreen />
                    <pl-valves:renderResultAsJson />
                </when>

PerformScreenValue

 public void invoke(PipelineContext pipelineContext) throws Exception {
        TurbineRunData rundata = getTurbineRunData(request);

        // 检查重定向标志,如果是重定向,则不需要将页面输出。
        if (!rundata.isRedirected()) {
            setContentType(rundata);

            Object result = null;

            try {
                result = performScreenModule(rundata);
            } finally {
                setOutputValue(pipelineContext, result);
            }
        }

        pipelineContext.invokeNext();
    }

    /** 执行screen模块。 */
    protected Object performScreenModule(TurbineRunData rundata) {
        ModuleFinder finder = new ModuleFinder(rundata.getTarget());

        // 如果设置了template,则默认打开layout
        rundata.setLayoutEnabled(true);

        try {
            Module module = finder.getScreenModule();

            // 当指定了templateName时,可以没有的screen module,而单单渲染模板。
            // 这样就实现了page-driven,即先写模板,必要时再写一个module class与之对应。
            if (module != null) {
                // 将event传入screen。
                ScreenEventUtil.setEventName(rundata.getRequest(), finder.event);

                try {

                    if (module instanceof ModuleReturningValue) {
                        return ((ModuleReturningValue) module).executeAndReturn();
                    } else {
                        module.execute();
                    }
                } finally {
                    ScreenEventUtil.setEventName(rundata.getRequest(), null);
                }
            } else {
                if (isScreenModuleRequired()) {
                    throw new ModuleNotFoundException("Could not find screen module: " + finder.moduleName);
                }
            }
        } catch (ModuleLoaderException e) {
            throw new WebxException("Failed to load screen module: " + finder.moduleName, e);
        } catch (Exception e) {
            throw new WebxException("Failed to execute screen: " + finder.moduleName, e);
        }

        return null;
    }

RenderResultAsJsonValve

public void invoke(PipelineContext pipelineContext) throws Exception {
        TurbineRunData rundata = getTurbineRunData(request);

        if (!rundata.isRedirected()) {
            Object resultObject = consumeInputValue(pipelineContext);

            if (resultObject == null) {
                return;
            }

            String javascriptVariable = getJavascriptVariable();
            boolean outputAsJson = javascriptVariable == null;

            if (outputAsJson) {
                // output as json
                response.setContentType(getContentType());
            } else {
                // output as javascript
                response.setContentType(getJavascriptContentType());
            }

            PrintWriter out = response.getWriter();
            String jsonResult = JSON.toJSONString(resultObject);

            if (outputAsJson) {
                out.print(jsonResult);
            } else {
                out.print("var ");
                out.print(javascriptVariable);
                out.print(" = ");
                out.print(jsonResult);
                out.print(";");
            }
        }

        pipelineContext.invokeNext();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值