如何将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文件,利用 ...
随机推荐
- Android开发时,那些相见恨晚的工具或网站!
本文来我在知乎话题Android开发时你遇到过什么相见恨晚的工具或网站?下的回答! 在实际Android开发过程确实会有很多相见恨晚的工具或网站出现,下面是我自己的一些分享. 1.源码网站 https ...
- linux yum list、search、-y、install、update、remove、grouplist、groupinstall、groupremove
redhat使用yum需要付费yum安装的也是rpm包 centos的网络yum源默认已经配置好了,连接的是centos官方yum源,在国外,网速慢 yum源配置在/etc/yum.repos.d下 ...
- java基础(九) 可变参数列表介绍
一.可变参数简介 在不确定参数的个数时,可以使用可变的参数列表. 1. 语法: 参数类型...(三个点) 例如: void printArray(Object...) 注意: 每个方法最多只有一个可变 ...
- 用unescape反编码得出汉字
var p="",s="4e00"; for( var i=0;i<255;i++){ p+=unescape(("\\u"+s).r ...
- LeetCode题解之Number of 1 Bits
1.题目描述 2.问题分析 使用C++ 标准库的 bitset 类,将整数转换为 二进制,然后将二进制表示转换为字符串,统计字符串中 1 的个数即可. 3.代码 int hammingWeight(u ...
- leetCode 题解之字符串中第一个不重复出现的字符
1.题目描述 Given a string, find the first non-repeating character in it and return it's index. If it doe ...
- [VS2008] [.NET 3.5] 如何解决 The imported project "C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CompactFramework.CSharp.targets" was not found
重新安装或者修复 NETCFv35PowerToys https://download.microsoft.com/download/f/a/c/fac1342d-044d-4d88-ae97-d27 ...
- [WinCE | VS2008 | Solution] VS2008 building WinCE projects taking a long time
1. Open C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CompactFramework.Common.targets 2. Find pa ...
- [翻译] AYVibrantButton
AYVibrantButton https://github.com/a1anyip/AYVibrantButton AYVibrantButton is a stylish button with ...
- python 下字符串格式时间比较
python 下有多个有关时间的模块,分别是time.datetime.calendar,今天重点讨论下time写法. 其中time模块,主要有以下方法: ltime=time.time() 获取当前 ...