Java Code Examples for freemarker.template.Template#setEncoding()
The following examples show how to use
freemarker.template.Template#setEncoding() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDFUtil.java From erp-framework with MIT License | 6 votes |
/** * 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; }
Example 2
Source File: AbstractGeneratorStrategy.java From sloth with Apache License 2.0 | 6 votes |
/** * 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 !! "); } }
Example 3
Source File: EasyUtils.java From boubei-tss with Apache License 2.0 | 6 votes |
/** * 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; } }
Example 4
Source File: GenerateTestCases.java From learn-pipeline-java with MIT License | 5 votes |
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); } }
Example 5
Source File: TemplateFactory.java From Mario with Apache License 2.0 | 5 votes |
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(); }