`
逆风的香1314
  • 浏览: 1453110 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

从零学习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 src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
分享到:
评论

相关推荐

    使用JSP技术构建电子商务应用系统-从入门到提高

    总结,本教程将带领读者从零开始,逐步掌握使用JSP构建电子商务应用的全过程,从基本概念到高级技巧,从设计用户界面到实现业务逻辑,再到性能优化和安全防范,全面涵盖JSP在电商系统中的应用。通过阅读提供的PDF...

    jsp开发指南(从基础到小项目实践)

    通过以上内容的学习,你可以从零开始,逐步建立起对JSP的深入理解,并能独立完成小规模的Web项目。同时,随着经验的积累,你还可以进一步探索更高级的Web开发技术和架构模式,提升自己的专业技能。

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    书中概念清晰、环环相扣,便于读者高效地学习。《深入浅出Struts2》适合Java Web 程序员阅读和参考,也可以作为计算机相关专业教材。 编辑推荐 《深入浅出Struts2》是广受赞誉的Struts2优秀教程.它全面而深入地...

    Spring Boot面试题(2022最新版)-重点

    如果不需要 Actuator 的安全性,可以通过配置属性 `management.endpoints.web.exposure.include=*` 和 `management.endpoints.web.exposure.exclude=*` 来控制暴露哪些端点。 **4.3 我们如何监视所有 Spring Boot ...

Global site tag (gtag.js) - Google Analytics