如何将freemarker文件转化为html文件
最近在做静态的页面报表服务,将前端生成的ftl文件转化为html格式的文件,供后面合成pdf使用。
freemarker基础可以参见:freemarker官方文档
前期准备:需要一个基础的ftl格式的文件。
一个freemarker中注入的对象
这里面单独命名了一个类:
/**
* 实体类
* @author Xia
*/
public class Person {
private String name;
private String tele;
private String email; public Person(String name, String tele, String email) {
this.name = name;
this.tele = tele;
this.email = email;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTele() {
return tele;
} public void setTele(String tele) {
this.tele = tele;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} }
具体的实现代码
static String templatePath = "/pdf0020ftlToHtml";
static String templateName = "part2.ftl";
static String targetHtmlPath = "src/main/resources/pdf0020ftlToHtml/part2.html"; public static void crateHTML(String templatePath, String templateName, String targetHtmlPath) {
FileWriter out = null; Person p = new Person("zhangsan", "13767682365", "qust@163.com"); try {
// 通过Configuration读取模版的配置文件
Configuration freemarkerCfg = new Configuration(Configuration.VERSION_2_3_23);
// 加载模版
// 设置要解析的模板所在的目录 这里面有三种设置的方式
// freemarkerCfg.setDirectoryForTemplateLoading(new File(templatePath));
// freemarkerCfg.setServletContextForTemplateLoading(servletContext, path); freemarkerCfg.setClassForTemplateLoading(Pdf0020ftlToHtml.class, templatePath);
// 设置默认的编码格式
freemarkerCfg.setDefaultEncoding("utf-8"); // 指定模版路径,并且获取模版
Template template = freemarkerCfg.getTemplate(templateName, "utf-8"); // 设置html静态页面输出路径
File f = new File(targetHtmlPath);
if (!f.exists()) {
f.createNewFile();
}
out = new FileWriter(f); template.process(p, out);
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
crateHTML(templatePath, templateName, targetHtmlPath);
}
注意,在web项目中可能会有乱码的情况。注意设置好响应的编码格式。
如何将freemarker文件转化为html文件的更多相关文章
- WPF: 读取XPS文件或将word、txt文件转化为XPS文件
读取XPS格式文件或将doc,txt文件转化为XPS文件,效果图如下: 1.XAML页面代码: <Window x:Class="WpfWord.MainWindow" xm ...
- WFP: 读取XPS文件或将word、txt文件转化为XPS文件
读取XPS格式文件或将doc,txt文件转化为XPS文件,效果图如下: 1.XAML页面代码: <Window x:Class="WpfWord.MainWindow" ...
- 怎样将word文件转化为Latex文件:word-to-latex-2.56具体解释
首先推荐大家读一读这篇博文:http://blog.csdn.net/ibingow/article/details/8613556 --------------------------------- ...
- jupyter命令把.ipynb文件转化为.py文件
jupyter nbconvert --to script *.ipynb 就能把当前文件夹下面的所有的.ipynb文件转化为.py文件
- 使用vivado将bit文件转化为mcs文件
使用vivado将bit文件转化为mcs文件 1.在Tcl Console中运行脚本: write_cfgmem -force -format MCS -size 64 -interface spix ...
- 将caj文件转化为pdf文件进行全文下载脚本(ubuntu下亲测有用)
最近ubuntu下caj阅读器,突然崩掉了,而偏偏要准备开题,在网上搜索原因未果,准备放弃时候,突然在网上看到一个脚本,说是很好用,可以在指定页面将caj文件转化为pdf文件,亲测有用,这里直接给出脚 ...
- Netlib文件转化为mps文件
Netlib文件转化为mps文件 简单方法1 下载并执行: git clone https://github.com/mtanneau/Netlib_experiments.git cd Netlib ...
- plink格式文件转化为vcf文件(VCF versions convert)
plink1.9版本支持转化为VCFv4.2格式 plink2.0版本支持转化为VCFv4.3格式 两个版本用到的命令不一样 对于plink1.9版本,转化为vcf文件的命令行为: plink --b ...
- C++ 利用 libxl 将 Excel 文件转化为 Xml 文件
在游戏开发工作中,策划和运营一般会用Excel来编写配置文件,但是程序读取配置,最方便的还是xml文件.所以最好约定一个格式,然后在二者之间做一个转化. 本文利用libxl来读取Excel文件,利用 ...
随机推荐
- Integer和new Integer
Java code ? 1 2 3 4 5 6 7 8 9 10 public static void main(String[] args) { Integer i1=10; I ...
- LeetCode 536----Construct Binary Tree from String
536. Construct Binary Tree from String You need to construct a binary tree from a string consisting ...
- Base64编码和解码实现
function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...
- python匿名函数 高阶函数 内置函数 文件操作
1.匿名函数 匿名就是没有名字 def func(x,y,z=1): return x+y+z 匿名 lambda x,y,z=1:x+y+z #与函数有相同的作用域,但是匿名意味着引用计数为0,使用 ...
- Centos 安装Dokuwiki
一.前言 DokuWiki是一个开源wiki引擎程序,运行于PHP环境下.DokuWiki程序小巧而功能强大.灵活,适合中小团队和个人网站知识库的管理. 二.环境 在centos6 下安装apache ...
- Prometheus Node_exporter 之 Basic CPU / Mem / Disk Gauge
1. CPU Busy :收集所有 cpu 内核 busy 状态占比 type: SinglestatUnit: perent(0-100)(所有 cpu使用情况 - 5分钟内 cpu 空闲的平均值) ...
- Mysql性能监控项及sql语句
推荐一款mysql监控软件MONyog 1.查询缓存: mysql> show variables like '%query_cache%'; 2.缓存在Cache中线程数量thread_cac ...
- teradata 查询创建表的时间
如何查询teradata创建表的时间? select tablename,CreateTimeStamp from dbc.tables ---dbc的table,user是在dbc内的吗?具体不清楚 ...
- C# 判断程序是否已经在运行
方式1: /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Main() { //获取欲启 ...
- MongoDB 安装和使用问题总结
1. 一直安装不了[一直next下去但最后没有发现生成文件夹] 去掉 Installing MongoDB Compass 前面的打勾 2. 需要开两个cmd运行mongodb 开第一个,输入以下运行 ...