Search in sources :

Example 26 with GroupTemplate

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

the class NativeTest method testSecurity.

@Test
public void testSecurity() throws Exception {
    StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
    Configuration cfg = Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    Template t = gt.getTemplate("hello,${@java.lang.System.currentTimeMillis()}");
    String str = t.render();
    AssertJUnit.assertEquals("hello,", 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) Test(org.testng.annotations.Test)

Example 27 with GroupTemplate

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

the class CompositeResourceLoaderTest method testSimple.

@Test
public void testSimple() throws Exception {
    Configuration conf = Configuration.defaultConfiguration();
    CompositeResourceLoader loader = new CompositeResourceLoader();
    String home = System.getProperty("user.dir");
    String path1 = home + "/src/test/resources/template/resourceloader/var1";
    String path2 = home + "/src/test/resources/template/resourceloader/var2";
    FileResourceLoader fileLoader1 = new FileResourceLoader(path1);
    FileResourceLoader fileLoader2 = new FileResourceLoader(path2);
    Map data = getData();
    // 根据id加载
    MapResourceLoader mapLoader = new MapResourceLoader(data);
    loader.addResourceLoader(new StartsWithMatcher("http:").withoutPrefix(), fileLoader2);
    loader.addResourceLoader(new StartsWithMatcher("db:").withoutPrefix(), mapLoader);
    loader.addResourceLoader(new AllowAllMatcher(), fileLoader1);
    GroupTemplate gt = new GroupTemplate(loader, conf);
    Template t = gt.getTemplate("/xxx.html");
    t.binding("a", "hello");
    String result = t.render();
    ;
    AssertJUnit.assertEquals("hellohello--file2:hello--db=hello", result);
}
Also used : StartsWithMatcher(org.beetl.core.resource.StartsWithMatcher) Configuration(org.beetl.core.Configuration) AllowAllMatcher(org.beetl.core.resource.AllowAllMatcher) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) CompositeResourceLoader(org.beetl.core.resource.CompositeResourceLoader) Map(java.util.Map) HashMap(java.util.HashMap) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 28 with GroupTemplate

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

the class GroupTemplateTest method main.

public static void main(String[] args) throws Exception {
    String home = System.getProperty("user.dir") + File.separator + "template" + File.separator;
    Configuration cf = Configuration.defaultConfiguration();
    cf.setStatementStart("<!--:");
    cf.setStatementEnd("-->");
    FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
    GroupTemplate gt = new GroupTemplate(rs, cf);
    List<StockModel> list = StockModel.dummyItems();
    Template t = gt.getTemplate("/helloworld.html");
    t.binding("items", list);
    StringWriter sw = new StringWriter();
    t.renderTo(sw);
    System.out.println(sw.toString());
    // 第二次
    t = gt.getTemplate("/helloworld.html");
    t.binding("items", list);
    sw = new StringWriter();
    t.renderTo(sw);
    System.out.println(sw.toString());
}
Also used : Configuration(org.beetl.core.Configuration) StringWriter(java.io.StringWriter) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 29 with GroupTemplate

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

the class ErrorTest method main.

public static void main(String[] args) throws Exception {
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
    Configuration cfg = Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    Template t = gt.getTemplate("/org/beetl/sample/s0125/error1.txt");
    String str = t.render();
    t = gt.getTemplate("/org/beetl/sample/s0125/error2.txt");
    str = t.render();
    System.out.println(str);
}
Also used : Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 30 with GroupTemplate

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

the class BeetlSpringView method renderMergedTemplateModel.

/* ----- ----- ----- ----- 实现方法 ----- ----- ----- ----- */
/**
 * 渲染指定视图
 *
 * @param model
 * @param request
 * @param response
 * @throws NoSuchBeanDefinitionException
 *             如果未设置GroupTemplate,且Spring上下文中也没有唯一的GroupTemplate bean
 * @throws NoUniqueBeanDefinitionException
 *             如果未设置GroupTemplate,且Spring上下文中有多个GroupTemplate bean
 */
@Override
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws NoSuchBeanDefinitionException, NoUniqueBeanDefinitionException {
    // 如果未指定groupTemplate,取上下文中唯一的GroupTemplate对象
    if (groupTemplate == null) {
        groupTemplate = getApplicationContext().getBean(GroupTemplate.class);
    }
    // 渲染方法
    WebRender render = new WebRender(groupTemplate) {

        @Override
        protected void modifyTemplate(Template template, String key, HttpServletRequest request, HttpServletResponse response, Object... args) {
            Map<?, ?> model = (Map<?, ?>) args[0];
            for (Entry<?, ?> entry : model.entrySet()) {
                String name = (String) entry.getKey();
                Object value = entry.getValue();
                template.binding(name, value);
            }
        }
    };
    String path = getUrl();
    render.render(path, request, response, model);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebRender(org.beetl.ext.web.WebRender) HttpServletResponse(javax.servlet.http.HttpServletResponse) GroupTemplate(org.beetl.core.GroupTemplate) Map(java.util.Map) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

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