SpringBoot + Freemarker 简单案例

1.加入Maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.ftl模板样例

<html>
<head>
    <title>freemarker测试</title>
</head>
<body>
<h1>${message},${name}</h1>
</body>
</html>

3.Java代码

try{
       //1、创建Configuration对象,指定编码集和模板文件夹
       Configuration configuration = new Configuration(Configuration.getVersion());
       configuration.setDefaultEncoding("utf-8");
       configuration.setDirectoryForTemplateLoading(new File("/Users/admin/web/test/ftl"));
       //2、创建Template对象,指定模板文件
       Template template = configuration.getTemplate("test.ftl");
       //3、准备数据,Map或POJO类型,推荐Map
       Map<String,Object> data = new HashMap<>();
       data.put("message", "第一个Freemarker例子");
       data.put("name", "jmm");
       //4、创建Writer对象,指定输出文件
       Writer writer = new FileWriter("/Users/admin/test.html");
       //5、生成模板
       template.process(data,writer);
}catch(Exception e){
     e.printStackTrace();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值