freemarker 定义模板layout

定义下面类, 会在以后模板中使用到

Java代码   收藏代码
  1. package com.vogella.freemarker.first;  
  2.   
  3. public class ValueExampleObject {  
  4.   
  5.   private String name;  
  6.   private String developer;  
  7.   
  8.   public ValueExampleObject(String name, String developer) {  
  9.     this.name = name;  
  10.     this.developer = developer;  
  11.   }  
  12.   
  13.   public String getName() {  
  14.     return name;  
  15.   }  
  16.   
  17.   public String getDeveloper() {  
  18.     return developer;  
  19.   }  
  20.   
  21. }   

 

 

Java代码   收藏代码
  1. package com.vogella.freemarker.first;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  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.Locale;  
  11. import java.util.Map;  
  12.   
  13. import freemarker.template.Configuration;  
  14. import freemarker.template.Template;  
  15. import freemarker.template.TemplateExceptionHandler;  
  16. import freemarker.template.Version;  
  17.   
  18. public class MainTest {  
  19.   
  20.   public static void main(String[] args) throws Exception {  
  21.   
  22.     // 1. Configure FreeMarker  
  23.     //  
  24.     // You should do this ONLY ONCE, when your application starts,  
  25.     // then reuse the same Configuration object elsewhere.  
  26.       
  27.     Configuration cfg = new Configuration();  
  28.       
  29.     // Where do we load the templates from:  
  30.     cfg.setClassForTemplateLoading(MainTest.class"templates");  
  31.       
  32.     // Some other recommended settings:  
  33.     cfg.setIncompatibleImprovements(new Version(2320));  
  34.     cfg.setDefaultEncoding("UTF-8");  
  35.     cfg.setLocale(Locale.US);  
  36.     cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);  
  37.   
  38.     // 2. Proccess template(s)  
  39.     //  
  40.     // You will do this for several times in typical applications.  
  41.       
  42.     // 2.1. Prepare the template input:  
  43.       
  44.     Map<String, Object> input = new HashMap<String, Object>();  
  45.       
  46.     input.put("title""Vogella example");  
  47.   
  48.     input.put("exampleObject"new ValueExampleObject("Java object""me"));  
  49.       
  50.     List<ValueExampleObject> systems = new ArrayList<ValueExampleObject>();  
  51.     systems.add(new ValueExampleObject("Android""Google"));  
  52.     systems.add(new ValueExampleObject("iOS States""Apple"));  
  53.     systems.add(new ValueExampleObject("Ubuntu""Canonical"));  
  54.     systems.add(new ValueExampleObject("Windows7""Microsoft"));  
  55.     input.put("systems", systems);  
  56.       
  57.     // 2.2. Get the template  
  58.   
  59.     Template template = cfg.getTemplate("helloworld.ftl");  
  60.         
  61.     // 2.3. Generate the output  
  62.   
  63.     // Write output to the console  
  64.     Writer consoleWriter = new OutputStreamWriter(System.out);  
  65.     template.process(input, consoleWriter);  
  66.   
  67.     // For the sake of example, also write output into a file:  
  68.     Writer fileWriter = new FileWriter(new File("output.html"));  
  69.     try {  
  70.       template.process(input, fileWriter);  
  71.     } finally {  
  72.       fileWriter.close();  
  73.     }  
  74.   
  75.   }  
  76. }   

 

 

定义layout--utils.ftl

 

Html代码   收藏代码
  1. <#macro page>  
  2.   <html>  
  3.   <head>  
  4.     <title>${title}</title>  
  5.   </head>  
  6.   <body>  
  7.     <h1>${title}</h1>  
  8.     
  9.     <#-- This processes the enclosed content:  -->  
  10.     <#nested>  
  11.   </body>  
  12.   </html>  
  13. </#macro>  
  14.   
  15. <#macro otherExample p1 p2>  
  16.   <p>The parameters were: ${p1}, ${p2}</p>  
  17. </#macro>   

 

 

创建helloworld.ftl 调用模板 layout--utils.ftl

 

Java代码   收藏代码
  1. <#import "lib/utils.ftl" as u>   
  2.   
  3. <@u.page>  
  4.   <p>${exampleObject.name} by ${exampleObject.developer}</p>  
  5.   
  6.   <ul>  
  7.     <#list systems as system>  
  8.       <li>${system_index + 1}. ${system.name} from ${system.developer}</li>  
  9.     </#list>  
  10.   </ul>  
  11.     
  12.   <#-- Just another example of using a macro: -->  
  13.   <@u.otherExample p1=11 p2=22 />  
  14. </@u.page>   

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值