下面列出了freemarker.template.Template#setEncoding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* freemarker渲染html
*/
public static <T> String freeMarkerRender(T data, String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码方式
Template template = freemarkerCfg.getTemplate(htmlTmp);
template.setEncoding("UTF-8");
// 合并数据模型与模板,将合并后的数据和模板写入到流中,这里使用的字符流
template.process(data, out);
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
/**
* base freemarker genarate method
* @param templateData
* @param templateFileRelativeDir
* @param templateFileName
* @param targetFileAbsoluteDir
* @param targetFileName
*/
private void gen(Object templateData, String templateFileRelativeDir, String templateFileName, String targetFileAbsoluteDir, String targetFileName){
try{
Configuration configuration = new Configuration();
configuration.setClassForTemplateLoading(Application.class, templateFileRelativeDir);
configuration.setObjectWrapper(new DefaultObjectWrapper());
Template template = configuration.getTemplate(templateFileName);
template.setEncoding(encoding);
if(!targetFileAbsoluteDir.endsWith(File.separator))
targetFileAbsoluteDir+=File.separator;
FileUtil.mkdir(targetFileAbsoluteDir);
Writer fw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(targetFileAbsoluteDir + targetFileName)),encoding));
template.process(templateData, fw);
}catch (Throwable e){
logger.error("Can not found the template file, path is \"" + templateFileRelativeDir + templateFileName +".\"");
e.printStackTrace();
throw new RuntimeException("IOException occur , please check !! ");
}
}
/**
* log4j里加 log4j.logger.freemarker=fatal 过滤掉FM自身的error Log
*/
public static String fmParse( String str, Map<String, ?> data, Writer out, Configuration cfg ) {
try {
cfg.setNumberFormat("#"); // 防止数字类格式化,eg: 1000 变 1,000
Template tl = new Template("t.ftl", new StringReader(str), cfg);
tl.setEncoding("UTF-8");
tl.process(data, out);
str = out.toString();
out.flush();
return str;
}
catch (Exception e) {
String errorMsg = "FM-parse-error:" + e.getMessage();
log.error(errorMsg);
log.debug("template = " + str + ", data = " + data);
return errorMsg;
}
}
private void generate(final String fileName) throws Exception {
config.setClassForTemplateLoading(GenerateTestCases.class,"/");
try (OutputStreamWriter writer =
new OutputStreamWriter(new FileOutputStream(new File("target", fileName)))){
Template template = config.getTemplate(fileName, "UTF-8");
template.setEncoding("UTF-8");
template.process(null, writer);
}
}
public String process(String path, Object context, String encoding)
throws IOException, TemplateException {
if (!init) {
conf.setDirectoryForTemplateLoading(file);
init = true;
}
Template template = conf.getTemplate(path);
if (StringUtils.isBlank(encoding)) {
template.setEncoding(encoding);
}
StringWriter sw = new StringWriter();
template.process(context, sw);
return sw.toString();
}