Freemarker学习记录——基础部分
来源:互联网 发布:用python做数据分析pdf 编辑:程序博客网 时间:2024/06/07 10:03
1、Freemarker介绍
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。目前企业中:主要用Freemarker做静态页面或是页面展示。
使用Freemarker所用到的jar下载地址:http://freemarker.sourceforge.net/freemarkerdownload.html
Maven坐标
<dependency><span style="white-space:pre"></span><groupId>freemarker</groupId><span style="white-space:pre"></span><artifactId>freemarker</artifactId><span style="white-space:pre"></span><version>2.3.18</version></dependency>
2、语法实例
- 首先创建一个java工程,并导入freemarker.jar包。再创建一个模板路径flt,并创建模板页面,创建一个静态页面存放路径freemarker。如图
图片代码
public class TestFM1 {public static void main(String[] args) throws Exception {// 实例化Freemarker的配置类Configuration con = new Configuration();// 第二步:给配置类设置路径File file = Paths.get("D:/java/TestFreemarker/flt/").toFile();con.setDirectoryForTemplateLoading(file);// 第三步:处理模板及数据之间 关系 将数据与模板合成一个html// 第四步: 输出htmlTemplate template = con.getTemplate("test1.html");File dFile = Paths.get("D:/java/TestFreemarker/freemarker/","freemarker.html").toFile();if (!dFile.exists()) {dFile.createNewFile();}Writer out = new FileWriter(dFile);// 定义数据Map<String, Object> rootMap = new HashMap<>();//输出hello worldString helloWorld = "Hello world !!";rootMap.put("world", helloWorld);template.process(rootMap, out);// 关闭流out.flush();out.close();}}
3、语法练习
- 输出hello world
//输出hello worldString helloWorld = "Hello world !!";rootMap.put("world", helloWorld);
<!-- 输出Hello world -->${world}
- 对象的输出
//输出对象属性User user = new User();user.setUsername("张三");user.setAge(25);rootMap.put("user", user);
<!-- 输出对象 --> 用户名 == ${user.username}<br/>年龄 == ${user.age}
- 遍历List
//遍历listList<String> listU = new ArrayList<>();listU.add("张三");listU.add("李四");listU.add("王五");listU.add("赵六");rootMap.put("listU", listU);
<!-- 遍历list --> <#list listU as str> ${str} </#list>
- 遍历map集合
//遍历MapMap<String,Object> map = new HashMap<>();map.put("aa", "aa....");map.put("bb", 12);map.put("cc", 34.12);map.put("dd", "ddddd....");rootMap.put("mymap", map);
<!-- 遍历map --> ${mymap.aa}/${mymap.bb}/${mymap.cc}/${mymap.dd} <#list mymap?keys as key> ${key} == ${mymap[key]} </#list>
- 遍历List<Map>
//遍历List<Map>List<Map<String, Object>> listMap = new ArrayList<>();Map<String,Object> map1 = new HashMap<>();map1.put("a1", "aaaaaaaaaa1");map1.put("b1", "bbbbbbbbbb1");map1.put("c1", "ccccccccccc1");Map<String,Object> map2 = new HashMap<>();map2.put("a1", "aaaaaaaaaa2");map2.put("b1", "bbbbbbbbbb2");map2.put("c1", "ccccccccccc2");listMap.add(map1);listMap.add(map2);rootMap.put("listMap", listMap);
<!-- 遍历List<Map> --> 第一种方法: <#list listMap as mymap> ${mymap.a1}//${mymap.b1}//${mymap.c1} </#list> 第二种: <#list listMap as mymap> <#list mymap?keys as key> ${key}=${mymap[key]} </#list> </#list>
- 获取索引
//获取索引List<String> listIndex = new ArrayList<>();listIndex.add("AAAA");listIndex.add("BBB");listIndex.add("CCCCCCC");listIndex.add("DD");rootMap.put("listIndex", listIndex);
<!-- 获取索引 --> <#list listIndex as ind> ${ind_index} = ${ind} </#list>
- 在模板中赋值
<!-- 在模板中赋值 --> <#assign x=0/> ${x} <#assign x="hahah"/> ${x} <#assign x="${world}"/> ${x} <#assign x> <#list ["zhouyi","zhouer","zhousan","zhousi","zhouwu"] as xx> ${xx} </#list> </#assign> ${x}
- #if标签中 == != || && 索引
<!-- #if == != && || 索引 --> <#list ["A","B","C","D","E","F","G","H"] as n> <#if n == "A"> 这个字母是:${n} </#if> <#if n != "A"> 这个字母不是A,而是:${n} </#if> </#list> <#list ["A","B","C","D","E","F","G","H"] as n> <#if n_index != 0> 索引不等于0,${n}的索引为:${n_index} </#if> </#list> <#list ["A","B","C","D","E","F","G","H"] as n> <#if (n_index == 0) || (n_index == 3)> ${n}的索引为:${n_index} </#if> </#list>
- #else标签
<!-- #else --> <#list ["A","B","C","D","E","F","G","H"] as n> <#if (n_index == 0) || (n_index == 3)> ${n}的索引为:${n_index} <#else> ${n_index} --> ${n} </#if> </#list>
- 时间格式
rootMap.put("nowD", new Date());
<!-- 时间格式 --> 1、date 2016-5-15 <span style="white-space:pre"></span>${nowD?date} 2、datetime 2016-5-15 17:28:30 <span style="white-space:pre"></span> ${nowD?datetime} 3、time 17:28:30 <span style="white-space:pre"></span> ${nowD?time}
- null处理
// null处理rootMap.put("vnull", null);
<!-- null处理 --> ${vnull!} ${vnull!"我是null"}
- 页面引入
<!- -将另一个页面引入本页面时可用以下命令完成 --><#include "/include/head.html">
0 0
- Freemarker学习记录——基础部分
- Freemarker学习记录——项目使用
- freemarker学习记录
- FreeMarker学习记录
- Java基础部分个人学习记录
- spark-DataFrame学习记录-[1]基础部分
- freemarker学习笔记《—》
- FreeMarker基础学习
- javascript学习——基础部分
- Java基础部分记录
- 2、HTML 学习记录——基础
- FreeMarker学习笔记(模板部分)
- RacingGame学习笔记2——基础图形部分1
- RacingGame学习笔记3——基础图形部分2
- RacingGame学习笔记4——基础图形部分3
- RacingGame学习笔记5——基础图形部分4
- RacingGame学习笔记6——基础图形部分5
- 【SQL学习】——第一部分:SQL基础
- mapreduce实现排序并且找出销量最多的数据
- 练习三1010
- 多态特点及弊端
- 第十一周项目50-Time类中的运算符重载
- memcache一致性hash原理
- Freemarker学习记录——基础部分
- Hadoop的nodemanager老是启动就挂掉解决方法以及如何查看hadoop日志
- 二叉树的基本操作
- 找数2--nkoj3679(百度面试题)
- Java集合框架Collection
- struts2_1:strust2的设计模式
- 用RVI方法,对iOS进行抓包
- linux下交叉编译jrtplib-3.9.1
- Android gson使用