两种静态化技术
freemarker(比velocity更强大)
velocity(出来的比较早)
--------------------------java
package com.dt;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
*@author dt
*@date 2017年12月9日 下午8:27:11
*@marker
*/
public class FreeMarkerTest {
public static void main(String[] args) throws Exception {
// 模板+数据模型=输出
Configuration configuration = new Configuration();
String dir="D:\\work\\space001\\freemarker-demo\\src\\main\\webapp\\";
//设置目录
configuration.setDirectoryForTemplateLoading(new File(dir));
//加载指定模板
Template t= configuration.getTemplate("freemarker.html");
//自带处理方法
Map root = new HashMap();
root.put("error", "测试error");
List<String> list = new ArrayList<String>();
list.add("1、内容一");
list.add("2、内容二");
list.add("3、内容三");
root.put("list", list);
Map mx = new HashMap();
mx.put("3", "范冰冰3");
mx.put("4", "范冰冰4");
mx.put("5", "范冰冰5");
root.put("mx", mx);
root.put("a", null);
root.put("ctime", new Date());
//输出
Writer out = new FileWriter(new File(dir+"hello.html"));
t.process(root, out);
System.out.println("生成完毕");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${error}<br>
<#list list as a>
${a}
</#list>
<#list mx?keys as key>
${mx[key]}
</#list>
<br>
${a!""}<br>
${ctime?datetime}
</body>
</html>
--------------------------------freemarker.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${error}<br>
<#list list as a>
${a}
</#list>
<#list mx?keys as key>
${mx[key]}
</#list>
<br>
${a!""}<br>
</body>
</html>
--------------------------hello.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
测试error<br>
1、内容一
2、内容二
3、内容三
范冰冰3
范冰冰5
范冰冰4
<br>
<br>
</body>
</html>