Search in sources :

Example 16 with GroupTemplate

use of org.beetl.core.GroupTemplate in project beetl2.0 by javamonkey.

the class JFinal3BeetlRenderFactory method config.

public void config(ResourceLoader rs) {
    if (groupTemplate != null) {
        groupTemplate.close();
    }
    try {
        Configuration cfg = Configuration.defaultConfiguration();
        groupTemplate = new GroupTemplate(rs, cfg);
    } catch (IOException e) {
        throw new RuntimeException("加载GroupTemplate失败", e);
    }
}
Also used : Configuration(org.beetl.core.Configuration) GroupTemplate(org.beetl.core.GroupTemplate) IOException(java.io.IOException)

Example 17 with GroupTemplate

use of org.beetl.core.GroupTemplate 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 18 with GroupTemplate

use of org.beetl.core.GroupTemplate in project swift by luastar.

the class BeetlUtils method init.

private void init() {
    try {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        groupTemplate = new GroupTemplate(resourceLoader, cfg);
    } catch (Exception e) {
        logger.error("转换为文件时异常", e);
    }
}
Also used : Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate)

Example 19 with GroupTemplate

use of org.beetl.core.GroupTemplate in project hello-world by haoziapple.

the class BasicUse method main.

public static void main(String[] args) throws Exception {
    StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
    Configuration cfg = Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    Template t = gt.getTemplate("hello,${name}");
    t.binding("name", "beetl");
    String str = t.render();
    System.out.println(str);
}
Also used : Configuration(org.beetl.core.Configuration) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 20 with GroupTemplate

use of org.beetl.core.GroupTemplate in project nutzboot by nutzam.

the class BeetlGroupTemplateStarter method getGroupTemplate.

@IocBean(depose = "close")
public GroupTemplate getGroupTemplate() throws IOException {
    Properties prop = new Properties();
    for (String key : conf.keySet()) {
        if (key.startsWith("beetl.")) {
            prop.put(key.substring("beetl.".length()), conf.get(key));
        }
    }
    System.out.println(prop);
    if (!prop.containsKey(Configuration.RESOURCE_LOADER)) {
        prop.put(Configuration.RESOURCE_LOADER, ClasspathResourceLoader.class.getName());
    }
    if (!prop.containsKey("RESOURCE.autoCheck")) {
        prop.put("RESOURCE.autoCheck", "true");
    }
    if (!prop.containsKey("RESOURCE.root")) {
        prop.put("RESOURCE.root", prop.getProperty("root", "template/"));
    }
    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视图就能正常工作了
        prop.put(Configuration.DIRECT_BYTE_OUTPUT, "true");
    }
    if (!prop.containsKey(Configuration.ERROR_HANDLER)) {
        // 没有自定义ERROR_HANDLER,用定制的
        prop.put(Configuration.ERROR_HANDLER, LogErrorHandler.class.getName());
    }
    Configuration cfg = new Configuration(prop);
    String local = conf.get(PROP_TEMPLATE_ROOT_LOCAL);
    GroupTemplate gt = new GroupTemplate(cfg);
    if (!Strings.isBlank(local)) {
        try {
            if (new File(local).exists()) {
                local = new File(local).getAbsolutePath();
                FileResourceLoader resourceLoader = new FileResourceLoader(local);
                resourceLoader.setAutoCheck(true);
                gt.setResourceLoader(resourceLoader);
                log.debugf("Template Local path=%s is ok, use it", local);
            }
        } catch (Throwable e) {
            log.infof("Template Local path=%s is not vaild, fallback to beetl.RESOURCE.root", local, e);
        }
    }
    return gt;
}
Also used : Configuration(org.beetl.core.Configuration) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) Properties(java.util.Properties) File(java.io.File) LogErrorHandler(org.beetl.ext.nutz.LogErrorHandler) IocBean(org.nutz.ioc.loader.annotation.IocBean)

Aggregations

GroupTemplate (org.beetl.core.GroupTemplate)31 Configuration (org.beetl.core.Configuration)22 Template (org.beetl.core.Template)19 ClasspathResourceLoader (org.beetl.core.resource.ClasspathResourceLoader)10 FileResourceLoader (org.beetl.core.resource.FileResourceLoader)7 IOException (java.io.IOException)5 Test (org.testng.annotations.Test)5 Map (java.util.Map)4 Properties (java.util.Properties)4 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)4 WebRender (org.beetl.ext.web.WebRender)4 HashMap (java.util.HashMap)3 InputStream (java.io.InputStream)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 WebAppResourceLoader (org.beetl.core.resource.WebAppResourceLoader)2 ToolUtil (com.ikoori.vip.server.core.util.ToolUtil)1 ActionContext (com.opensymphony.xwork2.ActionContext)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1