freemarker 模板引擎技术:
先上个图片
简单说就是莫办系统, 为了减少重复的代码, 更能降低数据库的压力,
优点: 速度快, 分担高并发, 降低服务器压力
缺点: 使用的局限性, 应用于模板固定的新闻页面, 商品详情页面等
文件后缀: XXX.ftl
使用到的技术: IO 流 ->文件的读取 ->输出生成的文件
使用步骤:
1. 创建一个 Configuration 对象 直接 new 一个对象. 构造方法的参数就是 freemaker 对应的版本号
Configuration configuration = new Configuration();
2. 设置模板文件使用的字符集. 一般就是 UTF-8 (这一步根据情况是设定)
3. 设置模板文件所在的路径
configuration.setDirectoryForTemplateLoading(new File("C:\\Demo\\pyg_project\\freemarkerDemo\\src\\main\\resources\\ftl"));
4. 加载一个模板, 创建一个模板对象
- Template template =
- configuration.getTemplate("文件名. ftl");
5. 创建一个模板使用的数据库集 一般是 Map
- Map<String, Object> rootMap = new HashMap<String, Object>();
- // 测试传入假数据
- rootMap.put("name", "张三");
- rootMap.put("message", "欢迎来到品优购商城");
6. 创建一个 Writer 对象 (这就是上面说的 io 流技术) 指定生成的文件名 ->输出
Writer out = new FileWriter(new File("生成的文件名. html 生成的文件类型"));
7. 调用模板对象的 process 方法输出文件
template.process(rootMap,out);
8. 关闭流
out.close();
以上是生成的代码:
需要的依赖
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.23</version>
- </dependency>
技术 Demo 及代码源码: 自己手写的
- package cn.itcast.core;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- import freemarker.template.TemplateException;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- public class TestFreemarker {
- public static void main(String[] args) throws IOException, TemplateException {
- //1. 创建 freemarker 初始化对象
- Configuration configuration = new Configuration();
- //2. 加载模板的所在目录位置
- configuration.setDirectoryForTemplateLoading(new File("C:\\Demo\\pyg_project\\freemarkerDemo\\src\\main\\resources\\ftl"));
- //3. 根据模板名称创建模板对象
- Template template =
- configuration.getTemplate("test.ftl");
- //4. 创建模拟的假数据
- Map<String, Object> rootMap = new HashMap<String, Object>();
- // 测试传入假数据
- rootMap.put("name", "张三");
- rootMap.put("message", "欢迎来到品优购商城");
- //5. 定义输出流 指定生成后的页面的位置极其名称
- ArrayList<String> numberList = new ArrayList<String>();
- numberList.add("张 1");
- numberList.add("张 2");
- numberList.add("张 3");
- numberList.add("张 4");
- numberList.add("张 5");
- rootMap.put("numberList", numberList);
- HashMap<String, String> persionMap = new HashMap<String, String>();
- persionMap.put("001", "张三 1");
- persionMap.put("002", "张三 2");
- persionMap.put("003", "张三 3");
- persionMap.put("004", "张三 4");
- rootMap.put("persionMap", persionMap);
- // 测试想模板中传入日期类型数据
- rootMap.put("today", new Date());
- //
- rootMap.put("point",1513513233);
- Writer out = new FileWriter(new File("test.html"));
- //6. 生成
- template.process(rootMap,out);
- //7. 关闭流
- out.close();
- }
- }
技术使用的 ftl 文件
- <HTML>
- <head>
- <meta charset="utf-8">
- <title>Freemarker 入门小 DEMO </title>
- </head>
- <body>
- <#-- 我只是一个注释 -->
${name}, 你好.${message}
- <#-- 在页面定义变量 -->
- <#assign linkman="周先生">
联系人:${linkman}
<#assign info = {"mobile":"1234548541633",'address':'北京市'}>
电话 : ${info.mobile} 地址:${info.address}
- <#-- 引入其他页面 -->
- <#include "head.ftl">
- <#-- 遍历集合 -->
- <#list numberList as number>
- ${number}
- <#if number_index == 0>
这是第一次循环
- <#else > 这是索引号 ${number_index}
- </#if>
- </#list>
- <#-- 遍历 Map 集合 -->
- <#list persionMap?keys as key>
- ${key} ${persionMap[key]}
- </#list>
- <#-- 演示展示日期类型数据 -->
当前日期:${today?date}<br>
当前时间:${today?time}<br>
当前日期加时间:${today?datetime}<br>
日期格式化:${today?string("yyyy 年 MM 月")}<br>
- <#-- 展示数值对象 -->
- ${
- point
- }
- <#-- 原样展示 -->
- ${
- point?c
- }
- <#-- 测试是否存在 -->
- <#if aaa??>
aaa 变量存在
- <#else>aaa 变量不存在
- </#if>
- <#-- 测试 ${aaa}是否存在 存在展示自己不存在展示! 后面的 -->
- ${aaa!"母鸡"}
- <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>
- <#if (n_index == 1) || (n_index == 3)>
${n} -- 红色
<#else>
${n} -- 绿色
- </#if>-
- </#list>
- </body>
- </HTML>
以上代码的技术知识点总结:(极其重要)
- <#list
- <#if
- <#else
- <#-- 引入其他页面 -->
- <#include "head.ftl">
- <#-- 在页面定义变量 -->
- <#assign linkman="周先生">
来源: http://www.bubuko.com/infodetail-3054100.html