1. 程式人生 > >beetl模板入門例子

beetl模板入門例子

2.2.3 source cti req pro strong odin sdn com

加入maven依賴

[html] view plain copy
  1. <dependency>
  2. <groupId>org.beetl</groupId>
  3. <artifactId>beetl-core</artifactId>
  4. <version>2.2.3</version>
  5. </dependency>


模板文件 src/main/resources/template/hello.btl 的內容為:

hello ${name}

你好:${name}

例子代碼如下:

[java] view plain copy
  1. package com.lala.template;
  2. import org.beetl.core.Configuration;
  3. import org.beetl.core.GroupTemplate;
  4. import org.beetl.core.Template;
  5. import org.beetl.core.resource.ClasspathResourceLoader;
  6. import org.beetl.core.resource.FileResourceLoader;
  7. import org.beetl.core.resource.StringTemplateResourceLoader;
  8. public class Beetl
  9. {
  10. /**
  11. * 加載classpath下的資源
  12. * @throws Exception
  13. */
  14. static void classpathRL()throws Exception
  15. {
  16. ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
  17. Configuration cfg = Configuration.defaultConfiguration();
  18. GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
  19. Template t = gt.getTemplate("/template/hello.btl");
  20. t.binding("name", "beetl");
  21. String str = t.render();
  22. System.out.println(str);
  23. }
  24. /**
  25. * 加載文件系統下的資源
  26. * @throws Exception
  27. */
  28. static void filePathRL()throws Exception
  29. {
  30. String root = "E:\\pp\\src\\camel-web\\src\\main\\resources\\template";
  31. FileResourceLoader resourceLoader = new FileResourceLoader(root,"utf-8");
  32. Configuration cfg = Configuration.defaultConfiguration();
  33. GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
  34. Template t = gt.getTemplate("hello.btl");
  35. t.binding("name", "beetl");
  36. String str = t.render();
  37. System.out.println(str);
  38. }
  39. /**
  40. * 直接構建字符串
  41. * @throws Exception
  42. */
  43. static void stringBeetl()throws Exception
  44. {
  45. StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
  46. Configuration cfg = Configuration.defaultConfiguration();
  47. GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
  48. Template t = gt.getTemplate("hello,${name} \r\n你好:${name}");
  49. t.binding("name", "beetl");
  50. String str = t.render();
  51. System.out.println(str);
  52. }
  53. }


servlet例子如下:

[java] view plain copy
    1. package com.lala.template;
    2. import java.io.IOException;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.annotation.WebServlet;
    5. import javax.servlet.http.HttpServlet;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import org.beetl.core.Configuration;
    9. import org.beetl.core.GroupTemplate;
    10. import org.beetl.core.Template;
    11. import org.beetl.core.resource.WebAppResourceLoader;
    12. @WebServlet("/beetl")
    13. public class BeetlServlet extends HttpServlet
    14. {
    15. private static final long serialVersionUID = 1L;
    16. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    17. {
    18. resp.setCharacterEncoding("UTF-8");
    19. resp.setContentType("text/html;charset=UTF-8");
    20. WebAppResourceLoader resourceLoader = new WebAppResourceLoader(this.getServletContext().getRealPath("/"), "UTF-8");
    21. Configuration cfg = Configuration.defaultConfiguration();
    22. GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    23. Template t = gt.getTemplate("/template/webhello.btl");
    24. t.binding("name", "beetl");
    25. t.renderTo(resp.getWriter());
    26. }
    27. }

beetl模板入門例子