实际上用程序语言编写的程序就是模板。 FTL (代表FreeMarker模板语言)。 这是为编写模板设计的非常简单的编程语言。
模板(FTL编程)是由如下部分混合而成的:
文本:文本会照着原样来输出。
插值:这部分的输出会被计算的值来替换。插值由 ${ 和 } 所分隔 (或者 #{ 和 },这种风格已经不建议再使用了)。
FTL标签:FTL标签和HTML标签很相似,但是它们却是给 FreeMarker 的指示, 而且不会打印在输出内容中。
注释:注释和HTML的注释也很相似,但它们是由 <#-- 和 -->来分隔的。注释会被 FreeMarker 直接忽略,更不会在输出内容中显示。
我们来看一个具体的模板:
<html>
<head>
<title>Book List</title>
</head>
<body>
<h1>${title}</h1>
<p>书籍列表:</p>
<#-- 打印书籍列表 -->
<table>
<tr>
<th>名称</th>
<th>价格</th>
</tr>
<#list books as book>
<tr>
<td>${book.name}</td>
<td>${book.price}</td>
</tr>
</#list>
</table>
</body>
</html>FTL 是区分大小写的。 list 是指令的名称而 List 就不是。类似地 ${name} 和 ${Name} 或 ${NAME} 也是不同的。比如:
<h1>${title}</h1>
<#-- 不等同 -->
<h1>${TITLE}</h1>
<h1>${Title}</h1>FTL 标签不可以在其他 FTL 标签和插值中使用。比如,这样做是错误的:
<#if <#include 'foo'>='bar'>...</#if>
${ <#if user='name'>...</#if> }注释可以放在 FTL 标签和插值中。比如:
<html>
<head>
<title>Book List</title>
</head>
<body>
<h1>${title}</h1>
<p>书籍列表:</p>
<table>
<tr>
<th>名称</th>
<th>价格</th>
</tr>
<#list books as book <#-- 打印书籍列表 -->>
<tr>
<td>${book.name <#-- 书名 -->}</td>
<td>${book.price <#-- 书价格 --> }</td>
</tr>
</#list>
</table>
</body>
</html>模版代码如下:
<html>
<head>
<title>Book List</title>
</head>
<body>
<h1>${title}</h1>
<p>书籍列表:</p>
<table>
<tr>
<th>名称</th>
<th>价格</th>
</tr>
<#list books as book <#-- 打印书籍列表 -->>
<tr>
<td>${book.name <#-- 书名 -->}</td>
<td>${book.price <#-- 书价格 --> }</td>
</tr>
</#list>
</table>
</body>
</html>Java 代码:
package com.hxstrive.freemarker.demo1;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;
public class Demo4 {
public static void main(String[] args) throws Exception {
// 1. 配置 FreeMarker
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
cfg.setClassForTemplateLoading(Demo4.class, "/templates/");
cfg.setDefaultEncoding("UTF-8");
cfg.setLocale(Locale.CHINESE);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
// 2. 准备模版数据
Map<String, Object> input = new HashMap<String, Object>();
List<Map<String,Object>> bookList = new ArrayList<Map<String,Object>>();
input.put("books", bookList);
input.put("title", "编程书籍推荐");
Map<String,Object> javaBook = new HashMap<String, Object>();
javaBook.put("name", "Java高级编程");
javaBook.put("price", 67.5f);
bookList.add(javaBook);
Map<String,Object> linuxBook = new HashMap<String, Object>();
linuxBook.put("name", "Linux入门到精通");
linuxBook.put("price", 36.8f);
bookList.add(linuxBook);
Map<String,Object> cssBook = new HashMap<String, Object>();
cssBook.put("name", "CSS的艺术");
cssBook.put("price", 44.5f);
bookList.add(cssBook);
Template template = cfg.getTemplate("template4.ftl");
Writer consoleWriter = new OutputStreamWriter(System.out);
template.process(input, consoleWriter);
// 将输出结果保存到文件中
Writer fileWriter = new FileWriter(new File("output.html"));
try {
template.process(input, fileWriter);
} finally {
fileWriter.close();
}
}
}输出结果:
<html> <head> <title>Book List</title> </head> <body> <h1>编程书籍推荐</h1> <p>书籍列表:</p> <table> <tr> <th>名称</th> <th>价格</th> </tr> <tr> <td>Java高级编程</td> <td>67.5</td> </tr> <tr> <td>Linux入门到精通</td> <td>36.8</td> </tr> <tr> <td>CSS的艺术</td> <td>44.5</td> </tr> </table> </body> </html>