FreeMarker中文文档Maven导入Freemarker的jar包:

在 https://mvnrepository.com/ 中搜索 freemarker。

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.30</version>
</dependency>

一、Bean对象

1、测试代码:

// FreeMarker模板所在路径
private static final String TEMPLATE_PATH = "src\\main\\resources\\ftl";
private static Configuration configuration = null;

@BeforeAll
static void init() throws IOException {
   configuration = new Configuration(Configuration.VERSION_2_3_30);
   configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
}

@Test
void test() throws Exception {
   UserEO eo = new UserEO("admin", "123456");
   Template template = configuration.getTemplate("Test.ftl");
   template.process(Maps.newHashMap("eo", eo), new PrintWriter(System.out));
}

2、模板代码:

${eo.username}:${eo.password}

3、结果:

admin:123456

二、Bean数组

1、测试代码:

ArrayList<UserEO> list = Lists.newArrayList(new UserEO("admin", "123456"), new UserEO("Test", "000000"));
Template template = configuration.getTemplate("Test.ftl");
template.process(Maps.newHashMap("datalist", list), new PrintWriter(System.out));

2、模板代码:

<#list datalist as eo>
${eo.username}:${eo.password}
</#list>
3、结果
admin:123456
Test:000000

三、Map对象

1、测试代码:

Map<Object, Object> map = Maps.newHashMap();
map.put("username", "Admin");
map.put("password", "123456");
Template template = configuration.getTemplate("Test.ftl");
template.process(Maps.newHashMap("map", map), new PrintWriter(System.out));

2、模板代码:

Map写法一:
${map.username}:${map.password}

Map写法二:
<#list map?keys as k>
   ${k}=${map[k]}
</#list>

3、结果:

Map写法一:
Admin:123456

Map写法二:
   password=123456
   username=Admin

四、Map数组

1、测试代码:

Map<Object, Object> map1 = Maps.newHashMap("username", "Admin","password", "123456");
Map<Object, Object> map2 = Maps.newHashMap("username", "Admin","password", "123456");
Map<Object, Object> map3 = Maps.newHashMap("username", "Admin","password", "123456");
List<?> datalist = Lists.newArrayList(map1,map2,map3);
Template template = configuration.getTemplate("Test.ftl");
template.process(Maps.newHashMap("datalist", datalist), new PrintWriter(System.out));

2、模板代码:

写法一:
<#list datalist as m>
 ${m.username}:${m.password}
</#list>

写法二:
<#list datalist as m>
 <#list m?keys as k>
  ${k}=${m[k]}
 </#list>
</#list>

3、结果:

写法一:
Admin:123456
Admin:123456
Admin:123456

写法二:
password=123456
username=Admin
password=123456
username=Admin
password=123456
username=Admin

五、迭代数组获取索引

1、测试代码:同上

2、模板代码:

<#list datalist as m>
 ${m_index}
</#list>

<#list datalist as m>
 <#if (m_index >2)>
 索引大于2
 </#if>
</#list>

注:

访问索引直接加上《 _index

表达式加上小括号是为了给表达式分组

3、结果:

写法二:
 0
 1
 2

六、赋值

1、测试代码:

Template template = configuration.getTemplate("Test.ftl");
template.process(Maps.newHashMap("test", "我是Test"), new PrintWriter(System.out));

2、模板代码:

<#assign a="${test}">
输出a=${a}
输出test=${test}

<#list ["A","B","C","D","E"] as item>
 ${item_index}=${item}
</#list>

3、结果:

输出a=我是Test
输出test=我是Test

 0=A
 1=B
 2=C
 3=D
 4=E


七、IFELSE

1、测试代码:

Template template = configuration.getTemplate("Test.ftl");
template.process(Maps.newHashMap("sex", 1, "name", "admin", "phone", "110"), new PrintWriter(System.out));

2、模板代码:

性别:<#if sex==1>男<#else>女</#if>
角色:<#if name="admin">管理员</#if>
报警:<#if phone="110">我要报警</#if>
3、结果:

性别:男
角色:管理员
报警:我要报警

八、格式化日期

1、测试代码:

Template template = configuration.getTemplate("Test.ftl");
template.process(Maps.newHashMap("cur_time", new Date()), new PrintWriter(System.out));

2、模板代码:

${cur_time?date}
${cur_time?datetime}
${cur_time?time}

3、结果:

2020-6-8
2020-6-8 19:54:32
19:54:32

九、NULL值处理

1、测试代码:

Template template = configuration.getTemplate("Test.ftl");
template.process(Maps.newHashMap("test", null), new PrintWriter(System.out));

2、模板代码:

${test!}
${test!"我是空"}

3、结果:


我是空