Пример #1
0
  @SuppressWarnings("unchecked")
  @Override
  protected void renderMergedTemplateModel(
      Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    BeetlGroupUtilConfiguration config =
        (BeetlGroupUtilConfiguration) this.getApplicationContext().getBean("beetlConfig");
    GroupTemplate group = config.getGroupTemplate();
    Template t = group.getFileTemplate(getUrl());
    group.enableNativeCall();
    Iterator<Entry<String, Object>> it = model.entrySet().iterator();
    Entry<String, Object> entry = null;
    while (it.hasNext()) {
      entry = it.next();
      t.set(entry.getKey(), entry.getValue());
    }
    WebVariable webVariable = new WebVariable();
    webVariable.setRequest(request);
    webVariable.setResponse(response);
    webVariable.setSession(request.getSession());

    t.set("servlet", webVariable);
    t.set("ctxPath", request.getContextPath());
    t.setRawValue("session", new SessionWrapper(request.getSession()));
    t.getText(response.getWriter());
  }
Пример #2
0
  public static void main(String[] args) throws Exception {

    String home = System.getProperty("user.dir") + File.separator + "template" + File.separator;
    GroupTemplate group = new GroupTemplate(new File(home));
    group.setCharset("GBK");
    group.config("<%", "%>", "${", "}");
    // group.config("<!--[", "]-->", "<!--[=", "]-->");
    group.enableChecker(0.01);
    group.registerFunction("str2json", new String2Json());

    group.setDebug(true);
    group.enableNativeCall();
    // group.enableDirectOutputByte();
    Map compileConfig = new HashMap();
    compileConfig.put(GroupTemplate.OPTIMIZE_KEEP_SOURCE, true);
    group.enableOptimize(compileConfig);
    //		 		 	group.enableOptimize();
    //		 		  group.enableDirectOutputByte();

    group.registerFunctionPackage("test", new NowFunction());
    Template template = group.getFileTemplate("/helloworld.html");
    template.set("tt", new Test());
    Map map = new HashMap();
    map.put("a", "a");
    map.put("b", "b");
    template.set("map", map);

    System.out.println(template.getTextAsString());

    template = group.getFileTemplate("/helloworld.html");
    template.set("tt", new Person());
    template.set("map", map);

    System.out.println(template.getTextAsString());
    //			template = group.getFileTemplate("/helloworld.html");
    //			template.set("a", 1);
    //		 	System.out.println(template.getTextAsString());
    //		 	group.removeTemplateCache("hello2");
    //		 	Template t = group.getStringTemplate("hello1dfdf2356${name}", "hello2");
    //		 	t.set("name", "中文");
    //		 	String str = t.getTextAsString();
    //		 	System.out.println(str);

    //

  }