if指令
- 在模板中,名称前面#号的标签为FreeMarker特有标签,称为“指令”。( 也称为FTL标签 )
文档位置: (1) The template at a glance https://freemarker.apache.org/docs/dgui_quickstart_template.html
(2) Directive Reference https://freemarker.apache.org/docs/ref_directives.html
- <#if> 指令用于实现条件输出,形如
<#if expression >
...
<#else>
...
</#if>
模板网页a1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<div>
名字 : ${name} <br>
年龄 : ${age?c} <br>
<#if (age<12) >
Warning: 年龄太小, 不可以骑小黄车!
<#else>
OK, 请扫码开锁!
</#if>
</div>
</body>
</html>
public class Test
{
public static void main(String[] args) throws TemplateException, IOException
{
// 初始化FreeMarker ( 模板库 )
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setDirectoryForTemplateLoading(new File("template")); // 指定模板根目录
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
// 创建数据模型(data-model)
Map<String,Object> model = new HashMap<String,Object>();
model.put("name", "li");// 加入model
model.put("age", 10);
// 提取模板 (template)
Template tp = cfg.getTemplate("a1.html"); // template/a1.html
// 合成输出 (output) : 直接输出到控制台显示
Writer out = new OutputStreamWriter(System.out);
tp.process(model, out);
}
}
结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<div>
名字 : li <br>
年龄 : 10 <br>
Warning: 年龄太小, 不可以骑小黄车!
</div>
</body>
</html>
list指令
- <#list> 指令用于对Container类型进行遍历
- Hash ( Map, HashMap)
- Sequence ( List, ArrayList, [] )
- Collection ( Set )
我们来学习一下 Sequence 类型进行遍历 ,形如:
<#list sequence as item>
...
</#list>
其中,sequence是变量名,item是迭代变量名。
模板网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<div>
<table>
<tr>
<th> 学号 </th>
<th> 姓名 </th>
<th> 性别 </th>
<th> 手机 </th>
</tr>
<#list students as row>
<tr>
<td> ${row.id?c} </td>
<td> ${row.name} </td>
<td> ${row.sex?then('男','女') } </td>
<td> ${row.cellphone} </td>
</tr>
</#list>
<table>
</div>
</body>
</html>
学生类
package my;
public class Student
{
private int id;
private String name;
private boolean sex;
private String cellphone;
public Student()
{
}
public Student(int id, String name, boolean sex, String cellphone)
{
this.id = id;
this.name = name;
this.sex = sex;
this.cellphone = cellphone;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public boolean isSex()
{
return sex;
}
public void setSex(boolean sex)
{
this.sex = sex;
}
public String getCellphone()
{
return cellphone;
}
public void setCellphone(String cellphone)
{
this.cellphone = cellphone;
}
}
测试
public class Test
{
public static void main(String[] args) throws TemplateException, IOException
{
// 初始化FreeMarker ( 模板库 )
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setDirectoryForTemplateLoading(new File("template")); // 指定模板根目录
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
// 创建数据模型(data-model)
Map<String,Object> model = new HashMap<String,Object>();
List<Student> students = new ArrayList<Student>();
students.add(new Student(20210001,"赵", true, "13810002939"));
students.add(new Student(20210002,"张", false, "13599998292"));
students.add(new Student(20210003,"王", true, "13810289008"));
students.add(new Student(20210004,"李", false, "18629238292"));
model.put("students", students);
// 提取模板 (template)
Template tp = cfg.getTemplate("a1.html"); // template/a1.html
// 合成输出 (output)
File file = new File("output/b1.html");
FileWriter out = new FileWriter(file);
tp.process(model, out);
System.out.println("输出HTML: " + file.getAbsolutePath());
}
}
结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<div>
<table>
<tr>
<th> 学号 </th>
<th> 姓名 </th>
<th> 性别 </th>
<th> 手机 </th>
</tr>
<tr>
<td> 20210001 </td>
<td> 赵 </td>
<td> 男 </td>
<td> 13810002939 </td>
</tr>
<tr>
<td> 20210002 </td>
<td> 张 </td>
<td> 女 </td>
<td> 13599998292 </td>
</tr>
<tr>
<td> 20210003 </td>
<td> 王 </td>
<td> 男 </td>
<td> 13810289008 </td>
</tr>
<tr>
<td> 20210004 </td>
<td> 李 </td>
<td> 女 </td>
<td> 18629238292 </td>
</tr>
<table>
</div>
</body>
</html>
- 对迭代变量也可以添加一些内置处理方法
参考 Built-in Reference | Loop variable built-ins https://freemarker.apache.org/docs/ref_builtins_loop_var.html
形如:
<#list sequence as item>
... ${item?index} ... 表示索引,从0开始
<#if row?is_first>
...
</#if> 判断是否首行
</#list>
- 对 Hash类型也可以遍历
参考 Directive Reference| list https://freemarker.apache.org/docs/ref_directive_list.html
<#list hash as key, value>
... ${key} ... ${value} ...
</#list>
include 指令
- <#include> 指令用于包含另外一个页面 通常用于包含公共页面,比如包含头部HTML
形如:
<#include “head.html” >
<#include “foot.html” parse=false encoding=”UTF-8” >
注意:
(1) 文件路径 :路径可以用相对路径,或绝对路径
(2) parse 参数用于指定是否解析 ,如果子页面不需要解析,则可以设定parse=false以加快处理速度
(3) encoding 参数用于指定目标页面的字符编码
(4) 给子页面传递参数 ,直接把参数值放到model里即可 <#global name=value > 参考 <#global> 标签的用法
例子:a1.html 包含book.html 和 color.html
a1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<div>
<#include "/default/book.html" >//相对于template
<#global color="红色" >
<#include "color.html" >//相对于a1.html
</div>
</body>
</html>
book.html
<div>
书名: ${book.title} <br>
作者: ${book.author} <br>
ISBN: ${book.isbn} <br>
书版社: ${book.press} <br>
订价: ${book.price} <br>
</div>
color.html
<div>
喜欢的颜色: ${color}
</div>
Book类
package my;
/* POJO: 只需要getter即可
*
*/
public class Book
{
public String title;
public String author;
public String isbn;
public String press;
public double getPrice()
{
return 50;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getIsbn()
{
return isbn;
}
public void setIsbn(String isbn)
{
this.isbn = isbn;
}
public String getPress()
{
return press;
}
public void setPress(String press)
{
this.press = press;
}
}
测试
public class Test
{
public static void main(String[] args) throws TemplateException, IOException
{
// 初始化FreeMarker ( 模板库 )
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setDirectoryForTemplateLoading(new File("template")); // 指定模板根目录
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
// 创建数据模型(data-model)
Map<String,Object> model = new HashMap<String,Object>();
// 可以添加一个POJO
Book b = new Book();
b.title = "C/C++";
b.author = "li";
b.isbn = "5645469891";
b.press = "大学出版社";
model.put("book", b);// 加入model
// 提取模板 (template) 相对于template
Template tp = cfg.getTemplate("/default/a1.html");
// 合成输出 (output) : 直接输出到控制台显示
Writer out = new OutputStreamWriter(System.out);
tp.process(model, out);
}
}
结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<div>
<div>
书名: C/C++ <br>
作者: li <br>
ISBN: 5645469891 <br>
书版社: 大学出版社 <br>
订价: 50 <br>
</div>//相对于template
<div>
喜欢的颜色: 红色
</div>//相对于a1.html
</div>
</body>
</html>
其他指令
了解其他的指令,使用的时候去查询 https://freemarker.apache.org/docs/ref_directives.html
比如,<#setting> 指令可以设定一些全局设置 ,比如设定日期的显示格式 <#setting date_format="yyyy/MM/dd">
模板网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<#setting date_format="yyyy/MM/dd">
<div>
今天是: ${today?date}
</div>
</body>
</html>
测试
public class Test
{
public static void main(String[] args) throws TemplateException, IOException
{
// 初始化FreeMarker ( 模板库 )
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setDirectoryForTemplateLoading(new File("template")); // 指定模板根目录
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
// 创建数据模型(data-model)
Map<String,Object> model = new HashMap<String,Object>();
model.put("today", new Date());// 加入model
// 提取模板 (template)
Template tp = cfg.getTemplate("a1.html"); // template/a1.html
// 合成输出 (output) : 直接输出到控制台显示
Writer out = new OutputStreamWriter(System.out);
tp.process(model, out);
}
}
结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<div>
今天是: 2021/04/10
</div>
</body>
</html>