从零学习freemarker(5)Include的使用

 http://www.java2000.net/p7861

Include的使用

代码


  1. package freemarker;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStreamWriter;
  5. import java.io.Writer;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import freemarker.template.Configuration;
  9. import freemarker.template.Template;
  10. public class TestInclude {
  11.   private Configuration cfg;
  12.   public Configuration getCfg() {
  13.     return cfg;
  14.   }
  15.   public void init() throws Exception {
  16.     cfg = new Configuration();
  17.     cfg.setDirectoryForTemplateLoading(new File("bin/freemaker"));
  18.   }
  19.   public static void main(String[] args) throws Exception {
  20.     TestInclude obj = new TestInclude();
  21.     obj.init();
  22.     Map root = new HashMap();
  23.     Template t = obj.getCfg().getTemplate("TestInclude.ftl");
  24.     Writer out = new OutputStreamWriter(new FileOutputStream("TestInclude.html"), "GBK");
  25.     t.process(root, out);
  26.     System.out.println("Successfull................");
  27.   }
  28. }
模板

  1. <html>
  2. <head>
  3.   <title>Test page</title>
  4. </head>
  5. <body>
  6.   <h1>Test page</h1>
  7.   <p>Blah blah...
  8. <#include "/TestInclude.copyright.html">
  9. </body>
  10. </html>  
TestInclude.copyright.html

  1. <hr>
  2. <i> Copyright (c) 2007-2008 <a href="http://www.java2000.net">JAVA世纪网</a><br>
  3. 版权所有. </i>
输出结果

  1. <html>
  2. <head>
  3.   <title>Test page</title>
  4. </head>
  5. <body>
  6.   <h1>Test page</h1>
  7.   <p>Blah blah...
  8. <hr>
  9. <i> Copyright (c) 2007-2008 <a href="http://www.java2000.net">JAVA世纪网</a><br>
  10. 版权所有. </i>
  11. </body>
  12. </html>  












<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值