首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

freemarker中兑现自定义标签(包含处理参数以及循环变量)

2012-10-31 
freemarker中实现自定义标签(包含处理参数以及循环变量)import java.io.OutputStreamWriterimport java.u

freemarker中实现自定义标签(包含处理参数以及循环变量)



import java.io.OutputStreamWriter;    import java.util.HashMap;    import java.util.Map;       /**    *     * 客户端测试模板输入类    */   public class RepeatTest {        public static void main(String[] args) {            Map<String,Object> root=new HashMap<String, Object>();               root.put("repeat", new RepeatDirective());                        FreeMarkertUtil.processTemplate("src/templates","repeat.ftl", "UTF-8", root, new OutputStreamWriter(System.out));                    }    }   import java.io.OutputStreamWriter;import java.util.HashMap;import java.util.Map;/** *  * 客户端测试模板输入类 */public class RepeatTest {public static void main(String[] args) {Map<String,Object> root=new HashMap<String, Object>();root.put("repeat", new RepeatDirective());FreeMarkertUtil.processTemplate("src/templates","repeat.ftl", "UTF-8", root, new OutputStreamWriter(System.out));}} 模板文件repeat.ftl如下:Java代码<#assign x = 1>       一个参数:    <@repeat count=4>      Test ${x}      <#assign x = x + 1>    </@repeat>       二个参数:    <@repeat count=3 hr=true>      Test    </@repeat>       循环变量:    <@repeat count=3; cnt>      ${cnt}. Test    </@repeat>     


输出结果:

Java代码
一个参数:   
  Test 1  
  Test 2  
  Test 3  
  Test 4  
  
二个参数:   
  Test   
<hr>  Test   
<hr>  Test   
  
循环变量:   
  1. Test   
  2. Test   
  3. Test

热点排行