Search in sources :

Example 1 with ScriptEvalError

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);
    }
}
Also used : ScriptEvalError(org.beetl.core.exception.ScriptEvalError) HashMap(java.util.HashMap) Map(java.util.Map) HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) IOException(java.io.IOException) BeetlException(org.beetl.core.exception.BeetlException)

Example 2 with ScriptEvalError

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);
    }
}
Also used : WebRender(org.beetl.ext.web.WebRender) HashMap(java.util.HashMap) ScriptEvalError(org.beetl.core.exception.ScriptEvalError) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ScriptEvalError

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;
}
Also used : WebRender(org.beetl.ext.web.WebRender) HashMap(java.util.HashMap) ScriptEvalError(org.beetl.core.exception.ScriptEvalError) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)3 Map (java.util.Map)3 ScriptEvalError (org.beetl.core.exception.ScriptEvalError)3 Iterator (java.util.Iterator)2 WebRender (org.beetl.ext.web.WebRender)2 IOException (java.io.IOException)1 BeetlException (org.beetl.core.exception.BeetlException)1 HTMLTagParserException (org.beetl.core.exception.HTMLTagParserException)1