java ftl 模板_Java模板引擎 - FreeMarker

http://freemarker.foofun.cn/

https://freemarker.apache.org/

https://mvnrepository.com/artifact/org.freemarker/freemarker

8d9ed72c9d292d33472e9a25ce8db94a.png

常用代码

@Test

public void deptemp() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {

String templateName = "deptemp.ftl";

String templatePath = "d:\\template";

String fileOutPath = "d:\\deptemp.txt";

Map map = initDatamydeptemp();

Configuration config = new Configuration();

config.setDefaultEncoding("UTF-8");

config.setDirectoryForTemplateLoading(new File(templatePath));

Template template = config.getTemplate(templateName);

template.process(map, new FileWriter(fileOutPath));

}

private Map initDatamydeptemp() {

List emps = new ArrayList<>();

Emp emp1 = new Emp("1","liubei");

Emp emp2 = new Emp("2","guanyu");

emps.add(emp1);

emps.add(emp2);

List depts = new ArrayList<>();

Dept dept1 = new Dept("1","jishubu",emps);

Dept dept2 = new Dept("2","caiwubu",emps);

depts.add(dept1);

depts.add(dept2);

//创建如下的测试数据

Map map = new HashMap();

//列表

map.put("depts", depts);

map.put("id", "001");

return map;

}

常用标签

1. 单值

${id}

2. 集合

部门ID:${dept.id}

部门名称:${dept.name}

员工ID:${emp.id}

员工名称:${emp.name}

#if>

#list>

#list>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值