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 IOExceptionTemplateException {

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 IOExceptionTemplateException {

/** 创建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();

}

}

Atitit freemarker模板总结 D:\workspace\springboothelloword\src\com\attilax\util\TempleteFreemarkerUtil.相关推荐

  1. SpringBoot (三) :SpringBoot使用Freemarker模板引擎渲染web视图

    什么是Freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而 ...

  2. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  3. Freemarker模板引擎

    模板引擎的实质就是将页面结构提前写好,然后将数据渲染到模板上生成一个静态页面,这样一来,下次就可以 直接访问静态文件,不用进行额外的获取数据的操作(例如:访问数据库),这样大大提升了网站的访问速度. ...

  4. freemarker模板生成pdf文件

    文章目录 1.pom依赖 2.ftl模板以及宋体文件 2.1.文件路径 2.2.ftl文件模板(test.ftl) 3.controller生成pdf文件 1.pom依赖 <!--freemar ...

  5. springboot结合Freemarker模板生成docx格式的word文档(附代码)

    首先参考的是这篇文章: java利用Freemarker模板生成docx格式的word文档(全过程) - 旁光 - 博客园参考:https://my.oschina.net/u/3737136/blo ...

  6. JavaWeb(14) 页面静态化之使用freemarker模板生成一个html静态页面

    题外话:        页面静态化(展示数据从JSP页面变成HTML页面)实现方式-->模板技术   从本质上来讲,模板技术是一个占位符动态替换技术.一个完整的模板技术需要四个元素:①模板语言( ...

  7. Freemarker模板引擎学习,生成html里的动态表格,可合并单元格

    需求:现有html模板,需动态填充数据,并且包含表格,表格大小不固定,根据数据多少确定表格大小. 解析:两种方案: 1.java代码实现:将模板文件读出为StringBuffer,找到特定位置,循环生 ...

  8. 使用freemarker模板引擎导出word文件

    目录 第一步:创建Word模板 第二步:整理好数据,核对好数据要填充的位置 第三步:使用工具类生成word文件 工具类: 第一步:创建Word模板 创建需要的模板 另存为:单一网页文件 文件名最好改为 ...

  9. freemarker 模板生成pdf文件并下载

    利用freemarker 模板生成pdf文件,通过浏览器直接下载或生成文件到指定目录 1.pom.xml文件 <!--引入Freemarker的依赖--> <dependency&g ...

  10. 使用freemarker模板生成带图片的word--xml格式

    文章目录 前言 一.制作freemarker模板 1. 准备模板 2.修改模板 二.后台代码 1.引入freemarker依赖 2. freemarker工具类方法 3. 测试方法 三.测试结果 总结 ...

最新文章

  1. Android Gradle 笔记
  2. 华为3com交换路由命令详解
  3. 大型分布式网站术语分析 15 条,你知道几条?
  4. 为什么构造函数不能声明为虚函数,析构函数可以,构造函数中为什么不能调用虚函数?
  5. c++类与类的依赖(Dependency)关系
  6. CentOS 7 搭建 LAMP
  7. python进程池的实现原理_Python基于进程池实现多进程过程解析
  8. Flex中动态更新List item
  9. 02-CSS基础与进阶-day6_2018-09-05-22-02-24
  10. 微理财与玖富小金票接口对接项目文档
  11. 干货分享 | 4万字全面解读数据中台、数据仓库、数据湖(建议收藏)
  12. Arthas结合Spring容器 线上排查Tips
  13. 王琪你计算机学院,计算机学院成功举办第四届青春飞扬大赛
  14. 小组取什么名字好_注册公司取什么名字好 八字起名大全
  15. 前端图片上传问题整理
  16. 怎么查快递单号,查询物流状态未签收单号
  17. 测试用例(教室丶椅子)
  18. Pytest测试框架的基本使用和allure测试报告
  19. Backblaze2022中期SSD故障质量报告解读
  20. 学生专用计算机怎么没声音,win7麦克风没声音怎么设置_电脑麦克风没声音怎么办...

热门文章

  1. 向量机和感知机的相同和不同点_感知机(perceptron)和支持向量机(svm)是一种东西吗? 如果不是那他们的区别和关系是什么?...
  2. java 10什么意思_详解:Java 10的10个新特性
  3. P3161 [CQOI2012]模拟工厂
  4. TOJ1017: Tour Guide
  5. Lucene 如何热备份
  6. 幸运的袋子(深度优先遍历(Depth First Search,DFS))
  7. 如何在 FineUIMvc 中引用第三方 JavaScript 库
  8. Spring Batch系列总括(转载)
  9. 编译安装mysql-5.5.33
  10. Redis容量及使用规划(转)