如何将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文件,利用 ...
随机推荐
- PHPCMS v9上传图片提示"undefined"的解决办法
把phpcms\modules\attachment\attachments.php中将 if(empty($this->userid)){改成 if(empty($_POST['userid' ...
- Apache安装完后加入系统服务的相关操作详解
Apache源码安装完毕后, 1.下面的脚本运行后就可以直接使用service apachexxx stop/start 来控制apache的启动与停止了! cp /usr/local/apache/ ...
- Codeforces 750 F:New Year and Finding Roots
传送门 首先如果一开始就找到了一个叶子,那么暴力去递归找它的父亲,每次随机一个方向(除了已知的儿子)走深度次,如果走到了一个叶子就不是这个方向 (设根的深度为 \(1\))这样子最后到达深度为 \(3 ...
- JavaScript对象遍历属性和值
原文链接:http://caibaojian.com/javascript-object-3.html 加入你输出来一个对象,但是苦于不知道里面有哪些属性和值,这个时候,你可以通过下面的代码来遍历这个 ...
- Spring Boot—11控制器Controller
package com.sample.smartmap.controller; import org.springframework.beans.factory.annotation.Autowire ...
- 从CVE-2018-1273看漏洞分析
漏洞分析的边界 漏洞分析最应该关注的是漏洞相关的代码,至于其余的代码可以通过关键位置下断点,来理解大概功能. 其中最关键的就是了解数据流,找到离漏洞位置最近的 原始数据 经过的位置,然后开始往下分析, ...
- git pull文件时和本地文件冲突的问题
在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...
- Mysql ibdata1简述
What is stored in ibdata1? 当启用innodb_file_per_table时,表存储在它们自己的表空间中,但共享表空间仍用于存储其他InnoDB的内部数据: 数据字典也就是 ...
- [WSUS] [Windows 10 Upgrade 1607/1703] 升级出错,出现 0xC1800118 或者卡在下载中…… 0%后失败
1. 安装 KB3159706 ,并进行安装后维护操作:https://support.microsoft.com/en-us/help/3159706/update-enables-esd-decr ...
- sonarQube环境搭建--常见问题及解决
环境配置:MySQL Server 5.7 Jdk1.8 1.安装mysql数据库(默认安装一路默认到底,注意不要先新建用户账号) a) Mysql 环境变量配置: b)新增my.ini文件: ...