use of org.beetl.core.exception.ScriptEvalError in project beetl2.0 by javamonkey.
the class GroupTemplate method runScript.
/**
* 执行某个脚本,参数是paras,返回的是顶级变量
* @param key
* @param paras
* @param w
* @param loader 额外的资源管理器就在脚本
* @return
* @throws ScriptEvalError
*/
public Map runScript(String key, Map<String, Object> paras, Writer w, ResourceLoader loader) throws ScriptEvalError {
Template t = loadScriptTemplate(key, loader);
t.fastBinding(paras);
if (w == null) {
t.render();
} else {
t.renderTo(w);
}
try {
Map map = getSrirptTopScopeVars(t);
if (map == null) {
throw new ScriptEvalError();
}
return map;
} catch (ScriptEvalError ex) {
throw ex;
} catch (Exception ex) {
throw new ScriptEvalError(ex);
}
}
use of org.beetl.core.exception.ScriptEvalError in project beetl2.0 by javamonkey.
the class WebSimulate method execute.
public void execute(HttpServletRequest req, HttpServletResponse rsp) {
String path = this.getValuePath(req);
RestPath restPath = this.getRealPath(path, req.getMethod().toLowerCase());
if (restPath == null) {
// 在没有逻辑处理的时候,直接渲染模板,
this.handleNullPath(req, rsp);
return;
}
String valueFile = restPath.path;
WebRender render = new WebRender(gt);
Map paras = this.getScriptParas(req, rsp);
String commonFile = getCommonValueFile(req, rsp);
Map commonData = new HashMap(), data = new HashMap();
try {
if (commonFile != null && gt.getResourceLoader().exist(commonFile)) {
commonData = gt.runScript(commonFile, paras);
}
paras.put("pathVars", restPath.values);
if (valueFile != null) {
data = gt.runScript(valueFile, paras);
}
} catch (ScriptEvalError e) {
throw new SimulateException("伪模型脚本有错!", e);
}
commonData.putAll(data);
if (commonData.containsKey("json")) {
// 认为是需要json请求
rsp.setContentType("text/json; charset=utf-8");
Object jsonData = commonData.get("json");
if (jsonData instanceof String) {
this.output((String) jsonData, rsp);
} else {
if (jsonUtil == null) {
throw new SimulateException("模拟属性采用了json,但没有设置JsonUtil");
}
String str = null;
try {
str = jsonUtil.toJson(jsonData);
} catch (Exception e) {
throw new SimulateException("序列化JSON出错", e);
}
this.output(str, rsp);
}
return;
} else {
// 如果是beetl ajax请求
String ajaxFlag = null;
if (data.containsKey("ajax")) {
ajaxFlag = (String) data.get("ajax");
}
Iterator it = commonData.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
Object value = commonData.get(key);
setValue(key, value, req);
}
String renderPath = null;
if (commonData.containsKey("view")) {
renderPath = (String) commonData.get("view");
} else {
renderPath = getRenderPath(req);
}
if (ajaxFlag != null) {
renderPath = renderPath + "#" + ajaxFlag;
}
render.render(renderPath, req, rsp);
}
}
use of org.beetl.core.exception.ScriptEvalError in project beetl2.0 by javamonkey.
the class WebSimulate method handleNullPath.
protected void handleNullPath(HttpServletRequest req, HttpServletResponse rsp) {
String commonFile = getCommonValueFile(req, rsp);
Map commonData = new HashMap();
if (commonFile != null && gt.getResourceLoader().exist(commonFile)) {
try {
commonData = gt.runScript(commonFile, new HashMap());
} catch (ScriptEvalError e) {
throw new SimulateException("伪模型脚本有错!", e);
}
}
Iterator it = commonData.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
Object value = commonData.get(key);
setValue(key, value, req);
}
String path = this.getRenderPath(req);
WebRender render = new WebRender(gt);
render.render(path, req, rsp, null);
return;
}
Aggregations