zoukankan      html  css  js  c++  java
  • beetl模板入门例子

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


    servlet例子如下:

    [java] view plain copy
     
      1. package com.lala.template;  
      2.   
      3. import java.io.IOException;  
      4.   
      5. import javax.servlet.ServletException;  
      6. import javax.servlet.annotation.WebServlet;  
      7. import javax.servlet.http.HttpServlet;  
      8. import javax.servlet.http.HttpServletRequest;  
      9. import javax.servlet.http.HttpServletResponse;  
      10.   
      11. import org.beetl.core.Configuration;  
      12. import org.beetl.core.GroupTemplate;  
      13. import org.beetl.core.Template;  
      14. import org.beetl.core.resource.WebAppResourceLoader;  
      15.   
      16. @WebServlet("/beetl")  
      17. public class BeetlServlet extends HttpServlet  
      18. {  
      19.     private static final long serialVersionUID = 1L;  
      20.   
      21.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
      22.     {  
      23.         resp.setCharacterEncoding("UTF-8");  
      24.         resp.setContentType("text/html;charset=UTF-8");  
      25.         WebAppResourceLoader  resourceLoader = new WebAppResourceLoader(this.getServletContext().getRealPath("/"), "UTF-8");          
      26.         Configuration cfg = Configuration.defaultConfiguration();  
      27.         GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);  
      28.         Template t = gt.getTemplate("/template/webhello.btl");    
      29.         t.binding("name", "beetl");       
      30.         t.renderTo(resp.getWriter());  
      31.     }  
      32. }  
  • 相关阅读:
    15年双11手淘前端技术分享(转)
    高程第9章 客户端检测
    高程8.4 screen对象 8.5history对象 8.6小结
    高程8.2location对象 8.3navigator对象
    高程第8章 BOM 8.1window对象
    高程 7.3 模仿块级作用域 7.4私有变量 7.5小结
    高程 第7章函数表达式 7.1递归 7.2闭包
    23、GoAccess分析Nginx日志
    11、Nginx反向代理服务
    10、LNMP架构
  • 原文地址:https://www.cnblogs.com/telwanggs/p/7064497.html
Copyright © 2011-2022 走看看