freemarker模板技术
来源:互联网 发布:淘宝联盟微店推广技巧 编辑:程序博客网 时间:2024/06/02 22:37
示例:
import freemarker.template.Configuration;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class Test ...{
public static Log log = LogFactory.getLog(Test.class);
public static void main(String[] args) ...{
try ...{
/**//*
定义中的文件内容 test.ftl
${title}
${author}
${content}
*/
Configuration cfg = new Configuration();
String tempDir="d:/test";//定义模板存放路径
//根据所在地方定义路径String tempDir= URLDecoder.decode(this.getClass().getClassLoader().getResource("../").getPath(), "UTF-8");
String tempName="test.ftl";//定义模板名
String outputDir="d:/test";//定义模板输出路径
String outputName="d:/test.txt";//定义模板输出文件名
cfg.setDirectoryForTemplateLoading(new File(tempDir));
Template temp = cfg.getTemplate(tempName, Locale.US, "UTF-8");
SimpleHash map = new SimpleHash();
map.put("title", "hello");
map.put("author", "beauty9235");
map.put("content", "hello,world!");
//定义将结果打印出来
StringWriter out = new StringWriter();
temp.process(map, out);
log.debug(out.toString());
//定义生成文件
File output = new File(outputDir, outputName);
Writer writer = new FileWriter(output);
try ...{
temp.process(map, writer);
} catch (TemplateException e) ...{
e.printStackTrace();
}
} catch (TemplateException t) ...{
log.debug(t.getMessage());
} catch (IOException i) ...{
log.debug(i.getMessage());
}
}
}
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class Test ...{
public static Log log = LogFactory.getLog(Test.class);
public static void main(String[] args) ...{
try ...{
/**//*
定义中的文件内容 test.ftl
${title}
${author}
${content}
*/
Configuration cfg = new Configuration();
String tempDir="d:/test";//定义模板存放路径
//根据所在地方定义路径String tempDir= URLDecoder.decode(this.getClass().getClassLoader().getResource("../").getPath(), "UTF-8");
String tempName="test.ftl";//定义模板名
String outputDir="d:/test";//定义模板输出路径
String outputName="d:/test.txt";//定义模板输出文件名
cfg.setDirectoryForTemplateLoading(new File(tempDir));
Template temp = cfg.getTemplate(tempName, Locale.US, "UTF-8");
SimpleHash map = new SimpleHash();
map.put("title", "hello");
map.put("author", "beauty9235");
map.put("content", "hello,world!");
//定义将结果打印出来
StringWriter out = new StringWriter();
temp.process(map, out);
log.debug(out.toString());
//定义生成文件
File output = new File(outputDir, outputName);
Writer writer = new FileWriter(output);
try ...{
temp.process(map, writer);
} catch (TemplateException e) ...{
e.printStackTrace();
}
} catch (TemplateException t) ...{
log.debug(t.getMessage());
} catch (IOException i) ...{
log.debug(i.getMessage());
}
}
}
public void geneHtmlFile(String templateFilePath, String htmlFilePath, SimpleHash simpleHash) { try { Configuration cfg = new Configuration(); cfg.setDefaultEncoding("UTF-8"); cfg.setOutputEncoding("UTF-8"); cfg.setLocale(Locale.US); cfg.setDirectoryForTemplateLoading(new File(StringUtils.substringBeforeLast(templateFilePath, "//"))); Template temp = cfg.getTemplate(StringUtils.substringAfterLast(templateFilePath, "//")); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(htmlFilePath)), "UTF-8")); temp.process(simpleHash,out); } catch (IOException e) { log.debug(e); } catch (TemplateException e) { log.debug(e); } } public void geneHtmlFile(String templateFilePath, String htmlFilePath, Map propMap) { try { Configuration cfg = new Configuration(); cfg.setDefaultEncoding("UTF-8"); cfg.setOutputEncoding("UTF-8"); cfg.setLocale(Locale.US); cfg.setDirectoryForTemplateLoading(new File(StringUtils.substringBeforeLast(templateFilePath, "//"))); Template temp = cfg.getTemplate(StringUtils.substringAfterLast(templateFilePath, "//")); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(htmlFilePath)), "UTF-8")); temp.process(propMap,out); } catch (IOException e) { log.debug(e); } catch (TemplateException e) { log.debug(e); } }
- freemarker模板技术
- FreeMarker 模板引擎技术
- Freemarker模板引擎技术
- 模板技术-freemarker与velocity
- 技术点-Freemarker-模板指导概念部分
- Mustache模板技术,一个比freemarker轻量级的模板引擎
- freemarker 模板
- FreeMarker模板
- freemarker-模板
- 基于Freemarker模板技术的分页组件设计
- 基于Freemarker模板技术的邮件发送模块设计
- 基于Freemarker模板技术的邮件发送模块设计
- 使用FreeMarker模板引擎作为Struts2的视图技术
- Struts2 下 基于Freemarker模板技术的分页组件设计
- 基于Freemarker模板技术的邮件发送模块设计
- 超详细 Java使用freemarker模板技术导出word
- freemarker模板中迭代Map
- FreeMarker解析字符串模板
- 动网论坛帖子调用一会显示一会不显示解决办法
- 12条标准决定你是富是穷!
- 获取数据库某张数据表的字段信息
- 直接在Servlet中获得spring bean context
- 给网站开发和设计者的10点建议[转]
- freemarker模板技术
- B Plan 第1步:产品需求设计之 2008-01-24
- 配置多个JSF配置文件
- 基于Spring+Hibernate的通用增删改查的实现方法
- 条形码
- 电话号码验证区号-号码-分机
- 终于建立了自己的blog
- 新手入门:Javascript的使用技巧大全
- 函数 sql