freemarker的简单入门程序

  本文主要介绍了freemarker的常用标签<#list>   <#import>  <#assign>  <#if> <#else> <#elseif>标签,并配有相应的案例。

在这里我想先介绍freemarker一个比较重要的概念:顶层变量:所谓顶层变量就是存在数据模型中的变量.

<#assign>标签的作用就是替换和创建顶层变量。

先看本项目的目录结果如下图所示:

其中FreeMarkerTest2是一个测试程序:

a.ftl和b.ftl是写freemarker标签的地方(.ftl文件就是freemarker template 文件中的意思,翻译成中文是:freemarker 模板文件)

FreeMarkeerTest2的代码如下:

 1 package com.supwisdom;
 2 
 3 import freemarker.template.Configuration;
 4 import freemarker.template.Template;
 5 
 6 import java.io.File;
 7 import java.io.PrintWriter;
 8 import java.util.ArrayList;
 9 import java.util.Date;
10 import java.util.HashMap;
11 import java.util.Map;
12 
13 /**
14  * Created by ${秦林森} on 2017/8/1.
15  */
16 public class FreeMarkerTest2 {
17     public static void main(String[] args) throws Exception{
18         //创建freemarker配置实例
19         Configuration cfg = new Configuration();
20         //这个template是目录中的文件夹名:
21         cfg.setDirectoryForTemplateLoading(new File("template"));
22         //创建数据模型
23         Map root=new HashMap<>();
24         ArrayList<Object> objects = new ArrayList<>();
25         for(int i=1;i<=7;i++){
26             objects.add("星期"+i);
27      }
28         HashMap<String, Address> map = new HashMap<>();
29         map.put("1",new Address("中国","北京"));
30         map.put("2",new Address("美国","华盛顿"));
31         map.put("3",new Address("尼日利亚","内罗毕"));
32         root.put("user","秦林森");
33         root.put("lis",objects);
34         root.put("date",new Date());
35         root.put("map",map);
36         //加载模板文件
37         Template template=cfg.getTemplate("b.ftl");
38         //显示生成数据:
39         PrintWriter out = new PrintWriter(System.out);
40         template.process(root,out);
41         out.flush();
42         out.close();
43     }
44 }

b.ftl的代码如下:其中我认为遍历map集合比较难,已用红色标出。

 1 <#assign user="林肯"><#--替换一个顶层变量-->
 2 ${user!}你是最棒的。you will conquer the world.
 3 <#assign user=4>
 4 
 5     <#list lis as n>
 6     ${n}
 7     </#list>
 8 <#list lis as a>
 9 ${a}
10 </#list>
11 
12 <#--时间的格式化输出-->
13 ${date?string("yyyy_MM_dd HH:mm:ss")}
14 <#assign user="仁慈"><#--创建一个顶层变量,所谓顶层变量就是直接放在数据模型中的值。-->
15 <#assign number=[1,2,3]><#--这句话相当于java语言中的int[] number=[1,2,3];赋值操作-->
16 ${number[0]}
17 <#assign subs="01234">
18 ${subs[0..1]}
19 
20 ${subs[1..4]}
21 <#--遍历map集合首先要得到keys-->
22 <#assign keys=map?keys>
23 <#list keys as c>
24   key:  ${c} 国家:${map["${c}"]["country"]} 城市:${map["${c}"].city}
25 </#list>
26 <#--这两种拼接字符串的效果是一样的-->
27 ${"hellooewio${user}"}
28 ${"hello"+user!}
29 
30 if else标签的使用:
31 <#--给a先赋值-->
32 <#assign a=7>
33 <#if a gt 6><#--这里推荐使用gt代替数学中的大于符号">"-->
34     a大于6
35 </#if>
36 if else使用:
37 <#assign b=5>
38 <#if b gt 6>
39     b 大于6
40 <#else>
41     b小于6
42 </#if>
43 if elseif else的使用:
44 <#assign c=4>
45 <#if c gt 4>
46     c大于4
47 <#elseif c lt 4>
48     c小于4
49 <#else>
50 c等于4.
51 </#if>
52 
53 freemarker的内建函数:
54 <#assign a="i you me">
55 <#assign b="I YOU ME">
56 <#assign c="<h>people</h>">
57 <#--内建函数cap_first,lower_case,upper_case-->
58 ${a?cap_first}<#--把a的第一个首字母变为大写-->
59 ${b?lower_case}<#--把b的字符串转化为小写-->
60 ${a?upper_case}<#--把a字符串转为大写-->
61 ${c?html}<#--对字符串进行hmtl编码,本例输出的结果是:&lt;h&gt;people&lt;/h&gt;-->
62 
63 空值处理运算符:! ??
64 ${oewoei!"人民"}<#--如果数据模型中不存在oewoei这个变量时,赋一个初始值"人民"-->
65 ${"oiieo"!}<#--如果数据模型中不存在oiieo这个变量时,赋一个初始值""-->
66 <#if oweio??><#--oweio在数据模型中存在时输出存在,反之输出不存在-->
67     oweio存在
68 <#else>
69 oweio不存在。
70 </#if>
71 include指令的使用,它相当于把一个文件的内容加到本文件中。
72 <#include "sixi.txt">
73 
74 自定义指令宏指令(没有任何参数的):
75 <#macro a>
76     <#if oweioo??>
77         存在
78         <#else>
79             不存在
80     </#if>
81 </#macro>
82 调用自定义指令:(无参的)
83 <@a></@a>
84 
85 <#macro m2 a b c >
86 ${a}+${b}+${c}
87 </#macro>
88 <@m2 a="老高" b="老张" c="老马" />
89 把a.ftl文件导入到b.flt中:
90 命名空间命名的:
91 一个重要的规则就是路径不应该包含大写字母,为了分隔词语,使用下划线_,就像
92 wml_form(而不是 wmlForm)。
93 
94 <#import "a.ftl" as aaa>
95 <#--调用a.ftl中的变量和方法(宏)-->
96 <@aaa.bo>
97     I am chinese
98 </@aaa.bo>
99 ${aaa.mail}

a.ftl文件的代码如下:

 1 <#--自定义bo这个宏-->
<#macro bo> 2 <table> 3 <tr> 4 <td> 5 <#nested> 6 </td> 7 <td> 8 <#nested> 9 </td> 10 </tr> 11 </table> 12 </#macro> 13 14 <@bo> 15 I love china. 16 I love LuAn 17 </@bo> 18 <#assign mail="123@qq.com">

 Address类的代码如下:

 1 package com.supwisdom;
 2 
 3 /**
 4  * Created by ${秦林森} on 2017/7/31.
 5  */
 6 public class Address {
 7     private String country;
 8     private String city;
 9 
10     public Address(String country, String city) {
11         this.country = country;
12         this.city = city;
13     }
14 
15     public String getCountry() {
16         return country;
17     }
18 
19     public void setCountry(String country) {
20         this.country = country;
21     }
22 
23     public String getCity() {
24         return city;
25     }
26 
27     public void setCity(String city) {
28         this.city = city;
29     }
30 }

具体的参考资料可通过下列网址下载:里面有尚学堂的视频和freemarker中文手册。

http://pan.baidu.com/s/1slJilAl

原文地址:https://www.cnblogs.com/1540340840qls/p/7269630.html