• 静态化模板Freemaker的基本语法


    首先,导入jar包

    freemark的demo类:

     1 public class FMDemo {
     2 
     3     //实例化Freemarker  配置类   ftl freemarker templet  
     4     // .java  .html  .jsp .xml
     5     // .ftl .txt 
     6     //dao bean xml service controller .jsp
     7     public static void main(String[] args) throws IOException, TemplateException {
     8         //模板 +  数据模型 = 输出
     9         Configuration  conf = new Configuration();
    10         //告诉conf 类  模板放在哪里
    11         String dir = "C:\Users\cuibin\workspace-juno\freemarker\ftl\";
    12         // 模板放在哪里
    13         conf.setDirectoryForTemplateLoading(new File(dir));    
    14         //模板对象
    15         Template template = conf.getTemplate("freemarker.html");
    16         //数据
    17         Map root = new HashMap();
    18         root.put("world", "世界你好");
    19         //1
    20         Person person = new Person();
    21         person.setId(1);
    22         person.setName("薪水");
    23         root.put("person", person);
    24         //2 List
    25         List<String> persons = new ArrayList<String>();
    26         persons.add("范冰冰");
    27         persons.add("李冰冰");
    28         persons.add("何灵");
    29         root.put("persons", persons);
    30         //3 Map
    31         Map mx = new HashMap();
    32         mx.put("fbb","范冰冰");
    33         mx.put("lbb","李冰冰");
    34         root.put("mx",mx);
    35         //4: List<Map>
    36         
    37         List<Map> maps = new ArrayList<Map>();
    38         Map pms1 = new HashMap();
    39         pms1.put("id1", "范冰冰");
    40         pms1.put("id2", "李冰冰");
    41         Map pms2 = new HashMap();
    42         pms2.put("id1", "曾志伟");
    43         pms2.put("id2", "何炅");
    44         maps.add(pms1);
    45         maps.add(pms2);
    46         root.put("maps", maps);
    47         
    48         //时间
    49         root.put("cur_time", new Date());
    50         //null
    51         root.put("val",null);
    52 
    53 
    54 
    55         //输出流  最终成文件
    56         Writer out = new FileWriter(new File(dir + "hello.html"));
    57         
    58         template.process(root, out);
    59         
    60         System.out.println("生成完毕!");
    61         
    62     }
    63 }

    模板页freemaker.html:

      1 <html>
      2 <head>
      3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      4 <title>Insert title here</title>
      5 </head>
      6 <body>
      7 ${world} <br/>
      8 
      9 ${person.id}/${person.name} <br/>
     10 
     11         <!-- 
     12         List<String> persons = new ArrayList<String>();
     13         persons.add("范冰冰");
     14         persons.add("李冰冰");
     15         persons.add("何灵");
     16         root.put("persons", persons); --><br/>
     17         
     18 <#list persons as person>
     19     ${person}
     20 </#list> <br/>
     21 
     22 <!--     Map mx = new HashMap();
     23         mx.put("fbb","范冰冰");
     24         mx.put("lbb","李冰冰");
     25         root.put("mx",mx); -->
     26 <#list mx?keys as key>
     27     ${mx[key]}
     28 </#list> <br/>
     29 
     30 ${mx.fbb}/${mx.lbb} <br/>
     31 
     32 <!--     List<Map> maps = new ArrayList<Map>();
     33         Map pms1 = new HashMap();
     34         pms1.put("id1", "范冰冰");
     35         pms1.put("id2", "李冰冰");
     36         Map pms2 = new HashMap();
     37         pms2.put("id1", "曾志伟");
     38         pms2.put("id2", "何炅");
     39         maps.add(pms1);
     40         maps.add(pms2);
     41         root.put("maps", maps); -->
     42 <#list maps as map >
     43     <#list map?keys as key>
     44         ${map[key]}
     45     </#list>
     46 </#list>        <br>
     47 <#list maps as map >
     48     ${map.id1}/${map.id2}
     49 </#list>
     50 <br/>
     51 
     52 <#list persons as p>
     53     ${p_index}
     54 </#list>
     55 
     56 <input type="hidden" value="${world}">
     57     <#assign x='${world}' />
     58         ${x}
     59         
     60     <#assign x>世界太好了</#assign>
     61         ${x}
     62         
     63     <#assign x>
     64        <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>
     65           ${n}
     66        </#list>
     67     </#assign>
     68        ${x}<br/>
     69        
     70      <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>
     71         <#if n != "星期一">
     72                ${n}
     73         </#if>
     74     </#list>
     75      <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>
     76         <#if n_index != 0>
     77            ${n}
     78         </#if>
     79     </#list><br/>
     80     
     81     <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>
     82         <#if (n_index == 1) || (n_index == 3)>
     83            ${n}
     84         </#if>
     85     </#list><br/>
     86     
     87     <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>
     88         <#if (n_index == 1) || (n_index == 3)>
     89             ${n} --红色
     90         <#else>
     91             ${n} --绿色
     92         </#if>
     93     </#list><br/>
     94     
     95     ${cur_time?datetime}<br/>
     96     
     97     ${val!}
     98     
     99     <!-- 分页 -->
    100     <#macro table pageNo>
    101         ${pageNo} 
    102         如果是每一页 <Strong>1</Strong>
    103         ...
    104     </#macro>
    105 <@table pageNo=8 /><br/>
    106 
    107     <#macro table u>
    108            ${u}
    109         <#nested/>
    110     </#macro>
    111 <@table u=8 >这是8</@table>
    112 
    113     
    114     
    115     
    116        
    117         
    118     
    119 
    120 
    121 
    122 </body>
    123 

    执行demo的main方法生成输出页:

      1 <html>
      2 <head>
      3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      4 <title>Insert title here</title>
      5 </head>
      6 <body>
      7 世界你好 <br/>
      8 
      9 1/薪水 <br/>
     10 
     11         <!-- 
     12         List<String> persons = new ArrayList<String>();
     13         persons.add("范冰冰");
     14         persons.add("李冰冰");
     15         persons.add("何灵");
     16         root.put("persons", persons); --><br/>
     17         
     18     范冰冰
     19     李冰冰
     20     何灵
     21  <br/>
     22 
     23 <!--     Map mx = new HashMap();
     24         mx.put("fbb","范冰冰");
     25         mx.put("lbb","李冰冰");
     26         root.put("mx",mx); -->
     27     李冰冰
     28     范冰冰
     29  <br/>
     30 
     31 范冰冰/李冰冰 <br/>
     32 
     33 <!--     List<Map> maps = new ArrayList<Map>();
     34         Map pms1 = new HashMap();
     35         pms1.put("id1", "范冰冰");
     36         pms1.put("id2", "李冰冰");
     37         Map pms2 = new HashMap();
     38         pms2.put("id1", "曾志伟");
     39         pms2.put("id2", "何炅");
     40         maps.add(pms1);
     41         maps.add(pms2);
     42         root.put("maps", maps); -->
     43         李冰冰
     44         范冰冰
     45         何炅
     46         曾志伟
     47         <br>
     48     范冰冰/李冰冰
     49     曾志伟/何炅
     50 <br/>
     51 
     52     0
     53     1
     54     2
     55 
     56 <input type="hidden" value="世界你好">
     57         世界你好
     58         
     59         世界太好了
     60         
     61                  星期一
     62           星期二
     63           星期三
     64           星期四
     65           星期五
     66           星期六
     67           星期天
     68 <br/>
     69        
     70                星期二
     71                星期三
     72                星期四
     73                星期五
     74                星期六
     75                星期天
     76                星期二
     77                星期三
     78                星期四
     79                星期五
     80                星期六
     81                星期天
     82     <br/>
     83     
     84                星期二
     85                    星期四
     86                 <br/>
     87     
     88             星期一 --绿色
     89                 星期二 --红色
     90                 星期三 --绿色
     91                 星期四 --红色
     92                 星期五 --绿色
     93                 星期六 --绿色
     94                 星期天 --绿色
     95     <br/>
     96     
     97     2015-5-11 15:39:02<br/>
     98     
     99     
    100     
    101     <!-- 分页 -->
    102         8 
    103         如果是每一页 <Strong>1</Strong>
    104         ...
    105 <br/>
    106 
    107            8
    108 这是8
    109 
    110     
    111     
    112     
    113        
    114         
    115     
    116 
    117 
    118 
    119 </body>
    120 </html>
  • 相关阅读:
    XML约束之DTD
    XML基础
    向方法传递多个参数
    从方法里返回值
    给方法传值
    对象状态和行为
    对象类型数组
    数组
    避开关键字
    primitive数据类型
  • 原文地址:https://www.cnblogs.com/cuibin/p/7076373.html
Copyright © 2020-2023  润新知