Pipeline.xml
<when>
<pl-conditions:target-extension-condition extension="json" />
<pl-valves:performScreen />
<pl-valves:renderResultAsJson />
</when>
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());
rundata.setLayoutEnabled(true);
try {
Module module = finder.getScreenModule();
if (module != null) {
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) {
response.setContentType(getContentType());
} else {
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();
}