Search in sources :

Example 1 with CookieFunction

use of org.beetl.ext.fn.CookieFunction in project beetl2.0 by javamonkey.

the class WebRender method render.

/**
 * @param key 模板资源id
 * @param request
 * @param response
 * @param args 其他参数,将会传给modifyTemplate方法
 */
public void render(String key, HttpServletRequest request, HttpServletResponse response, Object... args) {
    Writer writer = null;
    OutputStream os = null;
    String ajaxId = null;
    Template template = null;
    boolean isError = false;
    try {
        // response.setContentType(contentType);
        int ajaxIdIndex = key.lastIndexOf("#");
        if (ajaxIdIndex != -1) {
            ajaxId = key.substring(ajaxIdIndex + 1);
            key = key.substring(0, ajaxIdIndex);
            template = gt.getAjaxTemplate(key, ajaxId);
        } else {
            template = gt.getTemplate(key);
        }
        Enumeration<String> attrs = request.getAttributeNames();
        while (attrs.hasMoreElements()) {
            String attrName = attrs.nextElement();
            template.binding(attrName, request.getAttribute(attrName));
        }
        WebVariable webVariable = new WebVariable();
        webVariable.setRequest(request);
        webVariable.setResponse(response);
        template.binding(WebVariable.SESSION, new SessionWrapper(request, request.getSession(false)));
        template.binding(WebVariable.SERVLET, webVariable);
        template.binding(WebVariable.REQUEST, request);
        template.binding("ctxPath", request.getContextPath());
        template.binding("$page", new HashMap());
        template.binding("parameter", new ParameterWrapper(request));
        template.binding("cookie", new CookieFunction());
        modifyTemplate(template, key, request, response, args);
        String strWebAppExt = gt.getConf().getWebAppExt();
        if (strWebAppExt != null) {
            WebRenderExt renderExt = this.getWebRenderExt(strWebAppExt);
            renderExt.modify(template, gt, request, response);
        }
        if (gt.getConf().isDirectByteOutput()) {
            os = response.getOutputStream();
            template.renderTo(os);
        } else {
            writer = response.getWriter();
            template.renderTo(writer);
        }
    } catch (IOException e) {
        isError = true;
        handleClientError(e);
    } catch (BeetlException e) {
        isError = true;
        // response.setStatus(500);
        // 
        handleBeetlException(e);
    } finally {
        try {
            if (!isError && writer != null)
                writer.flush();
            if (!isError && os != null) {
                os.flush();
            }
        } catch (IOException e) {
            handleClientError(e);
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) IOException(java.io.IOException) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Writer(java.io.Writer) CookieFunction(org.beetl.ext.fn.CookieFunction)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 GroupTemplate (org.beetl.core.GroupTemplate)1 Template (org.beetl.core.Template)1 BeetlException (org.beetl.core.exception.BeetlException)1 CookieFunction (org.beetl.ext.fn.CookieFunction)1