Freemarker-2.3.22 Demo - No02_绑定单个参数

 

复制代码
package No02_绑定单个参数;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class Test {

    public static void main(String[] args) throws Exception {

        // 设置模板的home目录,默认从此目录获取模板(此设置只需在应用中设置一次) 
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setDirectoryForTemplateLoading(new File("D:/freemarker_template_home/"));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        
        // 获取模板
        Template temp = cfg.getTemplate("No02_绑定单个参数.ftl");
        
        // 创建、设置数据模型 
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("user", "Nick Huang");
        
        // 将模板和数据模型合并 
        String filename = temp.getName();
        filename = filename.substring(0, filename.lastIndexOf(".") + 1) + "html";
        FileOutputStream fos = new FileOutputStream(new File("D:/freemarker_output_home/" + filename));
        Writer out = new OutputStreamWriter(fos);
        
        temp.process(dataMap, out);
        out.flush();
    }

}
复制代码

 

No02_绑定单个参数.ftl
复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Good morning ${user}</title>
</head>
<body>
    Good morning ${user}, nice to meet you.
</body>
</html>
复制代码

 

posted @   nick_huang  阅读(438)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示