读书人

freemarker中兑现自定义标签(包含处理

发布时间: 2012-10-31 14:37:31 作者: rapoo

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

读书人网 >编程

热点推荐