静态化环境搭建Freemarker

两种静态化技术

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值