Search in sources :

Example 1 with WebRender

use of org.beetl.ext.web.WebRender 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 2 with WebRender

use of org.beetl.ext.web.WebRender 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)

Example 3 with WebRender

use of org.beetl.ext.web.WebRender in project beetl2.0 by javamonkey.

the class JFinal3BeetlRender method render.

@Override
public void render() {
    response.setContentType("text/html; charset=" + this.getEncoding());
    WebRender web = new WebRender(groupTemplate);
    web.render(this.view, this.request, this.response, null);
}
Also used : WebRender(org.beetl.ext.web.WebRender)

Example 4 with WebRender

use of org.beetl.ext.web.WebRender in project beetl2.0 by javamonkey.

the class BeetlViewMaker method init.

public void init() throws IOException {
    log.debug("beetl init ....");
    Configuration cfg = Configuration.defaultConfiguration();
    Properties prop = new Properties();
    InputStream ins = Configuration.class.getResourceAsStream("/beetl.properties");
    if (ins != null) {
        log.debug("found beetl.properties, loading ...");
        try {
            prop.load(ins);
        } finally {
            Streams.safeClose(ins);
        }
    }
    if (!prop.containsKey(Configuration.RESOURCE_LOADER)) {
        // 默认选用WebAppResourceLoader,除非用户自定义了RESOURCE_LOADER
        log.debug("no custom RESOURCE_LOADER found , select WebAppResourceLoader");
        cfg.setResourceLoader(WebAppResourceLoader.class.getName());
    }
    if (!prop.containsKey(Configuration.DIRECT_BYTE_OUTPUT)) {
        // 默认启用DIRECT_BYTE_OUTPUT,除非用户自定义, 一般不会.
        log.debug("no custom DIRECT_BYTE_OUTPUT found , set to true");
        // 当DIRECT_BYTE_OUTPUT为真时, beetl渲染会通过getOutputStream获取输出流
        // 而BeetlView会使用LazyResponseWrapper代理getOutputStream方法
        // 从而实现在模板输出之前,避免真正调用getOutputStream
        // 这样@Fail视图就能正常工作了
        cfg.setDirectByteOutput(true);
    }
    if (!prop.containsKey(Configuration.ERROR_HANDLER)) {
        // 没有自定义ERROR_HANDLER,用定制的
        cfg.setErrorHandlerClass(LogErrorHandler.class.getName());
    }
    groupTemplate = new GroupTemplate(cfg);
    render = new WebRender(groupTemplate);
    log.debug("beetl init complete");
}
Also used : WebRender(org.beetl.ext.web.WebRender) Configuration(org.beetl.core.Configuration) InputStream(java.io.InputStream) GroupTemplate(org.beetl.core.GroupTemplate) Properties(java.util.Properties) WebAppResourceLoader(org.beetl.core.resource.WebAppResourceLoader)

Example 5 with WebRender

use of org.beetl.ext.web.WebRender in project beetl2.0 by javamonkey.

the class ServletGroupTemplate method render.

public void render(String child, HttpServletRequest request, HttpServletResponse response) {
    WebRender render = new WebRender(groupTemplate);
    render.render(child, request, response);
}
Also used : WebRender(org.beetl.ext.web.WebRender)

Aggregations

WebRender (org.beetl.ext.web.WebRender)9 Map (java.util.Map)4 GroupTemplate (org.beetl.core.GroupTemplate)4 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Properties (java.util.Properties)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Configuration (org.beetl.core.Configuration)2 Template (org.beetl.core.Template)2 ScriptEvalError (org.beetl.core.exception.ScriptEvalError)2 ActionContext (com.opensymphony.xwork2.ActionContext)1 InputStream (java.io.InputStream)1 Entry (java.util.Map.Entry)1 ServletActionContext (org.apache.struts2.ServletActionContext)1 ClasspathResourceLoader (org.beetl.core.resource.ClasspathResourceLoader)1 WebAppResourceLoader (org.beetl.core.resource.WebAppResourceLoader)1 LogErrorHandler (org.beetl.ext.nutz.LogErrorHandler)1