zoukankan      html  css  js  c++  java
  • Freemarker-2.3.22 Demo

    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>
  • 相关阅读:
    3747 [POI2015]Kinoman
    1303 [CQOI2009]中位数图
    3769 [spoj 8549] BST again
    1015 [JSOI2008]星球大战starwar
    1193 [HNOI2006]马步距离
    合并Git仓库不相关历史版本解决方案
    vue-cli项目实现动态锚点定位
    jQuery加css3实现菜单栏组件(可无限添加子列表)
    JavaScript文件转成base64编码
    Ajax获取服务器响应头部信息
  • 原文地址:https://www.cnblogs.com/nick-huang/p/4610881.html
Copyright © 2011-2022 走看看