FreeMarker 的入门应用-基础篇

一.简介

FreeMarker 是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯 Java 编写,FreeMarker 被设计用来生成 HTML Web 页面,特别是基于 MVC 模式的应用程序,虽然 FreeMarker 具有一些编程的能力,但通常由 Java 程序准备要显示的数据,由FreeMarker 生成页面,通过模板显示准备的数据

FreeMarker 不是一个 Web 应用框架,而适合作为 Web 应用框架一个组件。FreeMarker 与容器无关,因为它并不知道 HTTP 或 Servlet;FreeMarker 同样可以应用于非Web应用程序环境,FreeMarker 更适合作为 Model2 框架(如 Struts)的视图组件,你也可以在模板中使用 JSP标记库。另外,FreeMarker是免费的。

2.应用场景

比较适合运用在访问量大(或页面数据量大),但是数据很少与后台进行交互(即对实时性要求不是很高的)的页面,比如商品网站上的商品详情页等。

3.前期准备

要想使用freemarker,首先必须要有freemarker的jar包,这个互联网上随处可以下载,这边就不多说

 
  1. <dependency>

  2. <groupId>freemarker</groupId>

  3. <artifactId>freemarker</artifactId>

  4. <version>2.3.9</version>

  5. </dependency>


 

4.入门demo

(1)创建一个testFreemarker类

 
  1. import java.io.File;

  2. import java.io.FileWriter;

  3. import java.io.Writer;

  4. import java.util.HashMap;

  5. import java.util.Map;

  6. import freemarker.template.Configuration;

  7. import freemarker.template.Template;

  8. /**

  9. * @作者:JackHisen(GWD)

  10. * @项目名:freemarker

  11. * @时间:2017-7-25 下午2:39:45

  12. * @version 1.0

  13. */

  14. public class testFreemarker {

  15. public static void main(String[] args) throws Exception {

  16. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  17. Configuration conf = new Configuration();

  18. //加载模板文件(模板的路径)

  19. conf.setDirectoryForTemplateLoading(new File(dir));

  20. // 加载模板

  21. Template template = conf.getTemplate("/freemarker-demo.ftl");

  22. // 定义数据

  23. Map root = new HashMap();

  24. root.put("world", "Hello World");

  25. // 定义输出

  26. Writer out = new FileWriter(dir + "/freemarker.html");

  27. template.process(root, out);

  28. System.out.println("转换成功");

  29. out.flush();

  30. out.close();

  31. }

  32. }

项目目录如下,其中freemarker.html文件是运行main函数后自动生成的,freemarker-demo.ftl为模板

(2)freemarker模板中写入内容

(3)生成的html页面:

5.其他数据类型

1.实体bean

(1)创建Person类

 
  1. public class Person {

  2. private int id;

  3. private String name;

  4. public int getId() {

  5. return id;

  6. }

  7. public void setId(int id) {

  8. this.id = id;

  9. }

  10. public String getName() {

  11. return name;

  12. }

  13. public void setName(String name) {

  14. this.name = name;

  15. }

  16. }

(2)testFreemarker

 
  1. public class testFreemarker {

  2. public static void main(String[] args) throws Exception {

  3. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  4. Configuration conf = new Configuration();

  5. //加载模板文件(模板的路径)

  6. conf.setDirectoryForTemplateLoading(new File(dir));

  7. // 加载模板

  8. Template template = conf.getTemplate("/freemarker-demo.ftl");

  9. // 定义数据

  10. Person person=new Person();

  11. person.setId(1);

  12. person.setName("小明");

  13. Map root = new HashMap();

  14. root.put("person", person);

  15. // 定义输出

  16. Writer out = new FileWriter(dir + "/freemarker.html");

  17. template.process(root, out);

  18. System.out.println("转换成功");

  19. out.flush();

  20. out.close();

  21. }

  22. }


(3)Freemarker模板

${person.id}
${person.name}


(4)生成的html页面

2.List集合

(1)testFreemarker

 
  1. public class testFreemarker {

  2. public static void main(String[] args) throws Exception {

  3. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  4. Configuration conf = new Configuration();

  5. //加载模板文件(模板的路径)

  6. conf.setDirectoryForTemplateLoading(new File(dir));

  7. // 加载模板

  8. Template template = conf.getTemplate("/freemarker-demo.ftl");

  9. // 定义数据

  10. Person p1=new Person();

  11. p1.setId(1);

  12. p1.setName("小明");

  13. Person p2=new Person();

  14. p2.setId(2);

  15. p2.setName("小华");

  16. List<Person> person=new ArrayList<Person>();

  17. person.add(p1);

  18. person.add(p2);

  19. Map root = new HashMap();

  20. root.put("person", person);

  21. // 定义输出

  22. Writer out = new FileWriter(dir + "/freemarker.html");

  23. template.process(root, out);

  24. System.out.println("转换成功");

  25. out.flush();

  26. out.close();

  27. }

  28. }


(2)模板

 
  1. <#list person as p>

  2. ${p.id}/${p.name}

  3. </#list>


(3)生成的html文件

3.Map集合

1.testFreemarker

 
  1. public class testFreemarker {

  2. public static void main(String[] args) throws Exception {

  3. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  4. Configuration conf = new Configuration();

  5. //加载模板文件(模板的路径)

  6. conf.setDirectoryForTemplateLoading(new File(dir));

  7. // 加载模板

  8. Template template = conf.getTemplate("/freemarker-demo.ftl");

  9. // 定义数据

  10. Map root = new HashMap();

  11. Map mxs = new HashMap();

  12. mxs.put("fbb","范冰冰");

  13. mxs.put("lbb","李冰冰");

  14. root.put("mxs",mxs);

  15. // 定义输出

  16. Writer out = new FileWriter(dir + "/freemarker.html");

  17. template.process(root, out);

  18. System.out.println("转换成功");

  19. out.flush();

  20. out.close();

  21. }

  22. }


2.模板(两种写法)

 
  1. ${mxs.fbb}/${mxs.lbb}

  2. <#list mxs?keys as k>

  3. ${mxs[k]}

  4. </#list>


3.生成的html

4.List<Map>集合

1.testFreemarker

 
  1. public class testFreemarker {

  2. public static void main(String[] args) throws Exception {

  3. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  4. Configuration conf = new Configuration();

  5. //加载模板文件(模板的路径)

  6. conf.setDirectoryForTemplateLoading(new File(dir));

  7. // 加载模板

  8. Template template = conf.getTemplate("/freemarker-demo.ftl");

  9. // 定义数据

  10. Map root = new HashMap();

  11. List<Map> maps = new ArrayList<Map>();

  12. Map pms1 = new HashMap();

  13. pms1.put("id1", "范冰冰");

  14. pms1.put("id2", "李冰冰");

  15. Map pms2 = new HashMap();

  16. pms2.put("id1", "曾志伟");

  17. pms2.put("id2", "何炅");

  18. maps.add(pms1);

  19. maps.add(pms2);

  20. root.put("maps", maps);

  21. // 定义输出

  22. Writer out = new FileWriter(dir + "/freemarker.html");

  23. template.process(root, out);

  24. System.out.println("转换成功");

  25. out.flush();

  26. out.close();

  27. }

  28. }

2.模板(2种)

 
  1. <#list maps as m>

  2. ${m.id1}/${m.id2}

  3. </#list>

  4. <#list maps as m>

  5. <#list m?keys as k>

  6. ${m[k]}

  7. </#list>

  8. </#list>


3.生成的html

5.获得当前迭代的索引

(1)testFreemarker

 
  1. public class testFreemarker {

  2. public static void main(String[] args) throws Exception {

  3. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  4. Configuration conf = new Configuration();

  5. //加载模板文件(模板的路径)

  6. conf.setDirectoryForTemplateLoading(new File(dir));

  7. // 加载模板

  8. Template template = conf.getTemplate("/freemarker-demo.ftl");

  9. // 定义数据

  10. Map root = new HashMap();

  11. Person p1=new Person();

  12. p1.setId(1);

  13. p1.setName("李冰冰");

  14. Person p2=new Person();

  15. p2.setId(2);

  16. p2.setName("范冰冰");

  17. Person p3=new Person();

  18. p3.setId(3);

  19. p3.setName("沙冰冰");

  20. List<Person> list = new ArrayList<Person>();

  21. list.add(p1);

  22. list.add(p2);

  23. list.add(p3);

  24. root.put("persons", list);

  25. // 定义输出

  26. Writer out = new FileWriter(dir + "/freemarker.html");

  27. template.process(root, out);

  28. System.out.println("转换成功");

  29. out.flush();

  30. out.close();

  31. }

  32. }


(2)模板

 
  1. <#list persons as p>

  2. ${p_index}

  3. </#list>


(3)生成的html文件

(6)在模板中进行赋值

(1)testFreemarker

 
  1. package com.gwd.freemarker;

  2. import java.io.File;

  3. import java.io.FileWriter;

  4. import java.io.Writer;

  5. import java.util.ArrayList;

  6. import java.util.HashMap;

  7. import java.util.List;

  8. import java.util.Map;

  9. import freemarker.template.Configuration;

  10. import freemarker.template.Template;

  11. /**

  12. * @作者:JackHisen(GWD)

  13. * @项目名:freemarker

  14. * @时间:2017-7-25 下午2:39:45

  15. * @version 1.0

  16. */

  17. public class testFreemarker {

  18. public static void main(String[] args) throws Exception {

  19. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  20. Configuration conf = new Configuration();

  21. //加载模板文件(模板的路径)

  22. conf.setDirectoryForTemplateLoading(new File(dir));

  23. // 加载模板

  24. Template template = conf.getTemplate("/freemarker-demo.ftl");

  25. // 定义数据

  26. Map root = new HashMap();

  27. root.put("world","hello world");

  28. // 定义输出

  29. Writer out = new FileWriter(dir + "/freemarker.html");

  30. template.process(root, out);

  31. System.out.println("转换成功");

  32. out.flush();

  33. out.close();

  34. }

  35. }


(2)模板

 
  1. <#assign x="${world}" />

  2. ${x}

  3. <#assign x>

  4. <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>

  5. ${n}

  6. </#list>

  7. </#assign>

  8. ${x}


(3)生成的html

7.if语句

(1)申明:一般情况下模板中的数据来源于后台,但是这边为了方便演示,所以数据都在模板中写死了,后台testFreemarker可以同上,但实际上map可以为空,只要确保能生成html页面即可

(2)模板

 
  1. <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>

  2. <#if n != "星期一">

  3. ${n}

  4. </#if>

  5. </#list>


 

(3)生成的html文件

8.else语句

(1)模板

 
  1. <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>

  2. <#if (n_index == 1) || (n_index == 3)>

  3. ${n} --红色

  4. <#else>

  5. ${n} --绿色

  6. </#if>

  7. </#list>

(2)生成的html

9.格式化日期

(1)testFreemarker

 
  1. public class testFreemarker {

  2. public static void main(String[] args) throws Exception {

  3. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  4. Configuration conf = new Configuration();

  5. //加载模板文件(模板的路径)

  6. conf.setDirectoryForTemplateLoading(new File(dir));

  7. // 加载模板

  8. Template template = conf.getTemplate("/freemarker-demo.ftl");

  9. // 定义数据

  10. Map root = new HashMap();

  11. root.put("cur_time",new Date());

  12. // 定义输出

  13. Writer out = new FileWriter(dir + "/freemarker.html");

  14. template.process(root, out);

  15. System.out.println("转换成功");

  16. out.flush();

  17. out.close();

  18. }

  19. }


(2)日期模板

${cur_time?date}

生成html

(3)日期时间模板

${cur_time?datetime}

生成html

(4)时间模板

${cur_time?time}


生成html

10.对null的处理

(1)testFreemarker

 
  1. public class testFreemarker {

  2. public static void main(String[] args) throws Exception {

  3. String dir="H:\\Java-EE Workspace\\freemarker\\src\\com\\gwd\\freemarker";

  4. Configuration conf = new Configuration();

  5. //加载模板文件(模板的路径)

  6. conf.setDirectoryForTemplateLoading(new File(dir));

  7. // 加载模板

  8. Template template = conf.getTemplate("/freemarker-demo.ftl");

  9. // 定义数据

  10. Map root = new HashMap();

  11. root.put("world",null);

  12. // 定义输出

  13. Writer out = new FileWriter(dir + "/freemarker.html");

  14. template.process(root, out);

  15. System.out.println("转换成功");

  16. out.flush();

  17. out.close();

  18. }

  19. }


(2)null为空模板

${world!}         ——前面有个null

生成html

(3)为null时给默认值模板

${world!"如果world为null,我就会显示"}

生成的html

11.宏定义

(1)普通宏定义

模板:

 
  1. <#macro table u>

  2. ${u}

  3. </#macro>

  4. <@table u="这个是宏定义" />


生成的html

(2)扩展宏定义

模板:

 
  1. <#macro table u>

  2. ${u}

  3. <#nested/>

  4. </#macro>

  5. <@table u=8 >我是扩展的宏定义</@table>

生成的html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾利克斯冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值