• Atitit freemarker模板总结 D:\workspace\springboothelloword\src\com\attilax\util\TempleteFreemarkerUtil.


    Atitit freemarker模板总结

     

    D:\workspace\springboothelloword\src\com\attilax\util\TempleteFreemarkerUtil.java

     

    package com.attilax.util;

     

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.OutputStreamWriter;

    import java.io.StringWriter;

    import java.util.Map;

     

    import org.apache.commons.io.FileUtils;

     

    import com.google.common.collect.Maps;

     

    import freemarker.template.Configuration;

    import freemarker.template.DefaultObjectWrapper;

    import freemarker.template.Template;

    import freemarker.template.TemplateException;

     

    public class TempleteFreemarkerUtil {

     

    public static void main(String[] args) throws IOException, TemplateException {

    String sql = "select distinst*from   ${name}  .";

    Map m_varRoot = Maps.newLinkedHashMap();

    m_varRoot.put("name", "tabled");

     

    String analysisTemplate = parseTmplt(sql, m_varRoot);

    System.out.println(analysisTemplate);

    }

     

    public static String parseTmplt(String tmplt, Map varRoot) throws IOException, TemplateException {

    String tmplt_dir = "D:\\000000\\";

    // new File() string byte转file是不存在的,,因为ipputstream读取file是个native方法

    java.util.Random r = new java.util.Random();

    String tmpleteId = String.valueOf(r.nextInt());

    File file = new File(tmplt_dir + tmpleteId);

     

    FileUtils.writeStringToFile(file, tmplt);

     

    String analysisTemplate = analysisTemplate(tmplt_dir, tmpleteId, "gbk", varRoot);

    return analysisTemplate;

    }

     

    /**

     * @param templateName

     *            模板文件名称

     * @param templateEncoding

     *            模板文件的编码方式

     * @param root

     *            数据模型根对象

     * @return

     * @throws IOException

     * @throws TemplateException

     */

    public static String analysisTemplate(String pathname, String templateName, String templateEncoding, Map<?, ?> root)

    throws IOException, TemplateException {

     

    /** 创建Configuration对象 */

    Configuration config = new Configuration();

    /** 指定模板路径 */

    // = "templates";

    File file = new File(pathname);

    /** 设置要解析的模板所在的目录,并加载模板文件 */

    config.setDirectoryForTemplateLoading(file);

    /** 设置包装器,并将对象包装为数据模型 */

    config.setObjectWrapper(new DefaultObjectWrapper());

    config.setStrictSyntaxMode(false);

     

    /** 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致 */

    Template template = config.getTemplate(templateName, templateEncoding);

    /** 合并数据模型与模板 */

    StringWriter stringWriter = new StringWriter();

    template.process(root, stringWriter);

    stringWriter.close();

    return stringWriter.toString();

     

    }

     

    }

     

     

    D:\0workspace\AtiPlatf_cms\src\com\attilax\rails\FreeMarkertUtil.java

     

     

    package templete;

     

    import java.io.File;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.OutputStreamWriter;

    import java.io.StringWriter;

    import java.util.Map;

     

    import org.apache.commons.io.FileUtils;

     

    import com.google.common.collect.Maps;

     

    import freemarker.template.Configuration;

    import freemarker.template.DefaultObjectWrapper;

    import freemarker.template.Template;

    import freemarker.template.TemplateException;

     

    public class freemarkerDemo {

     

    public static void main(String[] args) throws IOException, TemplateException {

    String sql="select distinst*from   ${name}  .";

    // new File()  string byte转file是不存在的,,因为ipputstream读取file是个native方法

    File file = new File("D:\\000000\\sql2.txt");

     

    FileUtils.writeStringToFile(file, sql);

    Map m=Maps.newConcurrentMap();m.put("name", "tabled");

    System.out.println(analysisTemplate("D:\\000000\\","sql2.txt","gbk",m));

    }

     

    /** @param templateName 模板文件名称

     * @param templateEncoding 模板文件的编码方式

     * @param root 数据模型根对象

     * @return 

     * @throws IOException

     * @throws TemplateException */

    public static String analysisTemplate(String pathname, String templateName, String templateEncoding, Map<?, ?> root ) throws IOException, TemplateException {

     

    /** 创建Configuration对象 */

    Configuration config = new Configuration();

    /** 指定模板路径 */

    // = "templates";

    File file = new File(pathname);

    /** 设置要解析的模板所在的目录,并加载模板文件 */

    config.setDirectoryForTemplateLoading(file);

    /** 设置包装器,并将对象包装为数据模型 */

    config.setObjectWrapper(new DefaultObjectWrapper());

    config.setStrictSyntaxMode(false);

     

     

    /** 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致 */

    Template template = config.getTemplate(templateName, templateEncoding);

    /** 合并数据模型与模板 */

      StringWriter stringWriter = new StringWriter();   

    template.process(root,stringWriter);

    stringWriter.close();

    return stringWriter.toString();

     

     

    }

     

    }

     

  • 相关阅读:
    截取表单提交的字符串信息转换成对象 -- 前端面试题(一)
    HTML5 简单归纳 -- 前端知识 (二)
    02_安装Linux
    01_Linux 简介
    Mysql学习笔记八:Mysql操作
    Mysql学习笔记七:常用SQL语句
    Mysql学习笔记六:事务
    Mysql学习笔记五:修改
    Mysql学习笔记四:查询
    Mysql学习笔记二:主键、外键
  • 原文地址:https://www.cnblogs.com/attilax/p/15197438.html
Copyright © 2020-2023  润新知