zoukankan      html  css  js  c++  java
  • Freemarker生成静态代码实例

      1、static.html

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
     2 <html>  
     3 <head>  
     4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     5 <title>Insert title here</title>  
     6 </head>  
     7 <body>  
     8     描述:${description}
     9   获取名字的多少:${names?size}
    10   名字的遍历:
    11   <#list names as name>
    12      这是第${name_index}个人,他是${name}
    13   <#if name=="李逍遥">
    14           他的武器是轩辕剑
    15   <#elseif name=="赵灵儿">
    16          她的武器是玉剑
    17   <#else>
    18     她的武器是紫金剑
    19   </#if>
    20  </#list>
    21  
    22  
    23  武器的遍历:
    24  <#list weaponMap?keys as key>
    25     key--->${key},武器名是${weaponMap[key]}
    26  </#list>
    27     
    28  ========================================================
    29    <#include "include.html">
    30    
    31 </body>
    32 </html>

     2、include.html

    include code

    3、测试类MyFreemarker.java

     1 package com.test.freemarker;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.OutputStreamWriter;
     6 import java.io.Writer;
     7 import java.util.ArrayList;
     8 import java.util.HashMap;
     9 import java.util.List;
    10 import java.util.Map;
    11 
    12 import freemarker.template.Configuration;
    13 import freemarker.template.DefaultObjectWrapper;
    14 import freemarker.template.Template;
    15 
    16 public class MyFreemarker {
    17 
    18     public static void main(String[] args) throws Exception{
    19         Configuration cfg = new Configuration();
    20         cfg.setDirectoryForTemplateLoading(new File("templates"));
    21         cfg.setDefaultEncoding("UTF-8");
    22         cfg.setObjectWrapper(new DefaultObjectWrapper()); 
    23         
    24         Map<String,Object> rootMap = new HashMap<String,Object>();
    25         rootMap.put("description", "来生成静态页面");
    26         
    27         List<String> names = new ArrayList<String>();
    28         names.add("李逍遥");
    29         names.add("赵灵儿");
    30         names.add("月如");
    31         
    32         rootMap.put("names", names);
    33         
    34         Map<String,Object> weaponMap = new HashMap<String, Object>();
    35         weaponMap.put("first", "轩辕剑");
    36         weaponMap.put("second", "玉剑");
    37         weaponMap.put("third", "紫金剑");
    38         
    39         rootMap.put("weaponMap", weaponMap);
    40         
    41         Template template = cfg.getTemplate("static.html");
    42         Writer writer = new OutputStreamWriter(new FileOutputStream("success.html"));
    43         template.process(rootMap , writer);
    44         System.out.println("静态页面生成了~~");
    45     }
    46 
    47 }


    结果:生成的页面success.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>Insert title here</title>  
    </head>  
    <body>  
        描述:来生成静态页面
      获取名字的多少:3
      名字的遍历:
         这是第0个人,他是李逍遥
              他的武器是轩辕剑
         这是第1个人,他是赵灵儿
             她的武器是玉剑
         这是第2个人,他是月如
        她的武器是紫金剑
     
     
     武器的遍历:
        key--->third,武器名是紫金剑
        key--->first,武器名是轩辕剑
        key--->second,武器名是玉剑
        
     ========================================================
    include code   
    </body>
    </html>
  • 相关阅读:
    C语言成长学习题(八)
    C语言成长学习题(七)
    C语言成长学习题(六)
    C语言成长学习题(五)
    Linux下zookeeper下载与安装教程
    Linux下mongoDB下载与安装
    并发容器之阻塞队列DelayQueue的使用案例及源码分析
    原子操作CAS-最小的线程安全
    ThreadLocal定义、使用案例及源码分析
    mac上使用git命令上传项目工程源码至Github/gitee
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5741601.html
Copyright © 2011-2022 走看看