FreeMarker应用一例——生成Java代码

对FreeMarker而言,无非是“Template + data-model = output”。 

这里,假设我们所要生成的Java代码,即“output”如下: 
Java代码   收藏代码
  1. package com.cs.qdog.swift.objects;  
  2.   
  3. public class F32B {  
  4.   
  5.     private Double amount;  
  6.     private String currency;  
  7.   
  8.     public Double getAmount() {  
  9.         return amount;  
  10.     }  
  11.   
  12.     public void setAmount(Double amount) {  
  13.         this.amount = amount;  
  14.     }  
  15.   
  16.     public String getCurrency() {  
  17.         return currency;  
  18.     }  
  19.   
  20.     public void setCurrency(String currency) {  
  21.         this.currency = currency;  
  22.     }  
  23. }  
  24.   
  25.    


其中,类名、属性都属于“data-model”,其可用树形结构表现如下: 

(root) 



+- class = “F32B” 



|- properties 

|   | 

|   +- currency 

|   |   | 

|   |   +- name = “currency” 

|   |   | 

|   |   +- type = “String” 

|   | 

|   +- amount 

|   |   | 

|   |   +- name = “amount” 

|   |   | 

|   |   +- type = “Double” 

那么,则可使用如下“Template”: 
Java代码   收藏代码
  1. package com.cs.qdog.swift.objects;  
  2.   
  3. public class ${class} {  
  4.   
  5.   <#list properties as prop>  
  6.     private ${prop.type} ${prop.name};  
  7.   </#list>  
  8.   
  9.   <#list properties as prop>  
  10.     public ${prop.type} get${prop.name?cap_first}(){  
  11.       return ${prop.name};  
  12.     }  
  13.     public void set${prop.name?cap_first}(${prop.type} ${prop.name}){  
  14.       this.${prop.name} = ${prop.name};  
  15.     }  
  16.   </#list>  
  17.   
  18.   
  19. }  
  20.   
  21.    





最后,将FreeMarker文档中的例子改动一下,测试生成“output”的Java程序如下: 
Java代码   收藏代码
  1. package com.cs.qdog.swift.objects;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.OutputStreamWriter;  
  6. import java.io.Writer;  
  7. import java.util.Collection;  
  8. import java.util.HashMap;  
  9. import java.util.HashSet;  
  10. import java.util.Map;  
  11.   
  12. import freemarker.template.Configuration;  
  13. import freemarker.template.DefaultObjectWrapper;  
  14. import freemarker.template.Template;  
  15. import freemarker.template.TemplateException;  
  16.   
  17. public class GenObjects {  
  18.   
  19.     public static void main(String[] args) throws IOException,  
  20.             TemplateException {  
  21.   
  22.         /* ------------------------------------------------------------------- */  
  23.         /* You usually do it only once in the whole application life-cycle: */  
  24.   
  25.         /* Create and adjust the configuration */  
  26.         Configuration cfg = new Configuration();  
  27.         cfg.setDirectoryForTemplateLoading(new File(  
  28.                 "D:/Temp/EclipseWorkSpace/GenSwiftFields/templates"));  
  29.         cfg.setObjectWrapper(new DefaultObjectWrapper());  
  30.   
  31.         /* ------------------------------------------------------------------- */  
  32.         /* You usually do these for many times in the application life-cycle: */  
  33.   
  34.         /* Get or create a template */  
  35.         Template temp = cfg.getTemplate("SwiftFieldClass.ftl");  
  36.   
  37.         /* Create a data-model */  
  38.         Map<String, Object> root = new HashMap<String, Object>();  
  39.         root.put("class""F32B");  
  40.         Collection<Map<String, String>> properties = new HashSet<Map<String, String>>();  
  41.         root.put("properties", properties);  
  42.   
  43.         /* subfield 1: currency */  
  44.         Map<String, String> currency = new HashMap<String, String>();  
  45.         currency.put("name""currency");  
  46.         currency.put("type""String");  
  47.         properties.add(currency);  
  48.   
  49.         /* subfield 2: amount */  
  50.         Map<String, String> amount = new HashMap<String, String>();  
  51.         amount.put("name""amount");  
  52.         amount.put("type""Double");  
  53.         properties.add(amount);  
  54.   
  55.         /* Merge data-model with template */  
  56.         Writer out = new OutputStreamWriter(System.out);  
  57.         temp.process(root, out);  
  58.         out.flush();  
  59.     }  
  60.   
  61. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值