FreemarkerUtil工具类
来源:互联网 发布:linux基础入门教程 编辑:程序博客网 时间:2024/06/07 01:05
package com.mmiku.basic.util;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map;
import org.apache.commons.io.output.FileWriterWithEncoding;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreemarkerUtil {
private static FreemarkerUtil util;
private static Configuration cfg;
private FreemarkerUtil() {
}
public static FreemarkerUtil getInstance(String pname) {
if(util==null) {
cfg = new Configuration();
cfg.setClassForTemplateLoading(FreemarkerUtil.class, pname);
cfg.setDefaultEncoding("utf-8");
util = new FreemarkerUtil();
}
return util;
}
private Template getTemplate(String fname) {
try {
return cfg.getTemplate(fname);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 通过标准输出流输出模板的结果
* @param root 数据对象
* @param fname 模板文件
*/
public void sprint(Map<String,Object> root,String fname) {
try {
Template template = getTemplate(fname);
template.setEncoding("utf-8");
template.process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 基于文件的输出
* @param root
* @param fname
* @param outpath
*/
public void fprint(Map<String,Object> root,String fname,String outpath) {
try {
Template template = getTemplate(fname);
template.setEncoding("utf-8");
template.process(root, new FileWriterWithEncoding(outpath, "utf-8"));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map;
import org.apache.commons.io.output.FileWriterWithEncoding;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreemarkerUtil {
private static FreemarkerUtil util;
private static Configuration cfg;
private FreemarkerUtil() {
}
public static FreemarkerUtil getInstance(String pname) {
if(util==null) {
cfg = new Configuration();
cfg.setClassForTemplateLoading(FreemarkerUtil.class, pname);
cfg.setDefaultEncoding("utf-8");
util = new FreemarkerUtil();
}
return util;
}
private Template getTemplate(String fname) {
try {
return cfg.getTemplate(fname);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 通过标准输出流输出模板的结果
* @param root 数据对象
* @param fname 模板文件
*/
public void sprint(Map<String,Object> root,String fname) {
try {
Template template = getTemplate(fname);
template.setEncoding("utf-8");
template.process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 基于文件的输出
* @param root
* @param fname
* @param outpath
*/
public void fprint(Map<String,Object> root,String fname,String outpath) {
try {
Template template = getTemplate(fname);
template.setEncoding("utf-8");
template.process(root, new FileWriterWithEncoding(outpath, "utf-8"));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 0
- FreemarkerUtil工具类封装
- FreemarkerUtil工具类
- FreeMarkerUtil
- FreeMarkerUtil
- 工具类
- 工具类!!!!
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- php 文件操作
- Android数据存储恢复机制
- 国情观察团
- Codeforces-697B Barnicle
- PCA的数学原理(非常值得阅读)!!!!其实更重要的应该是实际意义
- FreemarkerUtil工具类
- Android开发——后台获取用户点击位置坐标(可获取用户支付宝密码)
- grunt-contrib-uglify参数和使用
- Spring Security-01-初体验
- 安卓手机root
- csdnbug
- sql注入扫描器——sqlmap
- HDU 4302 线段树
- Spring Security-02-关于登录