FreeMarkerUtl

本文介绍了一个使用FreeMarker模板引擎生成HTML页面的Java工具类。该工具类通过指定模板路径、模板文件名、数据源及目标文件路径来生成HTML文件。文章详细展示了如何配置FreeMarker并处理可能出现的异常。

/**
* @title FreeMarkerUtl
* @description 模板文件工具类
* @author maohuidong
* @date 2017-07-05
*/
public class FreeMarkerUtl {

/**
* @function genHtmlPage
* @param templatePath:模板路径 tempalteFile:模板文件名称 root:模板文件需要渲染的数据 genFilePath:生成的文件路径和名称
* @description 用户提交问卷
* @return (无)
* @throws Exception
* @author maohuidong
* @date 2017-07-05
*/
public static Boolean genHtmlPage(String templatePath,String tempalteFile,Map<Object, Object> root,String genFilePath){
try {
Configuration configuration = new Configuration();
//模板文件的路径
configuration.setDirectoryForTemplateLoading(new File(templatePath));
//字符集
configuration.setDefaultEncoding("utf-8");

//获取模板
Template template = configuration.getTemplate(tempalteFile, "utf-8");

//创建writer对象
File file = new File(genFilePath);
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
template.process(root, writer);
writer.flush();
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (TemplateException e) {
e.printStackTrace();
return false;
}
return true;
}
}

转载于:https://www.cnblogs.com/maohuidong/p/7853288.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值