freemarker自定义标签
假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html
1、错误描述
1 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 2 严重: Template processing error: "Error executing macro: write\nrequired parameter: nums is not specified." 3 4 Error executing macro: write 5 required parameter: nums is not specified. 6 The problematic instruction: 7 ---------- 8 ==> macro write [on line 11, column 10 in myself.ftl] 9 in user-directive write [on line 18, column 10 in myself.ftl] 10 ---------- 11 12 Java backtrace for programmers: 13 ---------- 14 freemarker.template.TemplateException: Error executing macro: write 15 required parameter: nums is not specified. 16 at freemarker.core.Macro$Context.sanityCheck(Macro.java:211) 17 at freemarker.core.Macro$Context.runMacro(Macro.java:169) 18 at freemarker.core.Environment.visit(Environment.java:614) 19 at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106) 20 at freemarker.core.Environment.visit(Environment.java:221) 21 at freemarker.core.MixedContent.accept(MixedContent.java:92) 22 at freemarker.core.Environment.visit(Environment.java:221) 23 at freemarker.core.Environment.process(Environment.java:199) 24 at freemarker.template.Template.process(Template.java:259) 25 at com.you.freemarker.FreemarkerTemplate.printFtl(FreemarkerTemplate.java:77) 26 at com.you.test.freemarker.FreemarkerTest.studentPrint(FreemarkerTest.java:217) 27 at com.you.test.freemarker.FreemarkerTest.testMyself(FreemarkerTest.java:204) 28 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 29 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 30 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 31 at java.lang.reflect.Method.invoke(Unknown Source) 32 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 33 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 34 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 35 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 36 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 37 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 38 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 39 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 40 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 41 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 42 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 43 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 44 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 45 at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 46 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 47 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 48 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 49 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 50 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 51 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 52 53 <html> 54 <head> 55 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 56 <title>freemarker自定义标签</title> 57 58 </head> 59 60 <body> 61 62 63 Error executing macro: write 64 required parameter: nums is not specified. 65 The problematic instruction: 66 ---------- 67 ==> macro write [on line 11, column 10 in myself.ftl] 68 in user-directive write [on line 18, column 10 in myself.ftl] 69 ---------- 70 71 Java backtrace for programmers: 72 ---------- 73 freemarker.template.TemplateException: Error executing macro: write 74 required parameter: nums is not specified. 75 at freemarker.core.Macro$Context.sanityCheck(Macro.java:211) 76 at freemarker.core.Macro$Context.runMacro(Macro.java:169) 77 at freemarker.core.Environment.visit(Environment.java:614) 78 at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106) 79 at freemarker.core.Environment.visit(Environment.java:221) 80 at freemarker.core.MixedContent.accept(MixedContent.java:92) 81 at freemarker.core.Environment.visit(Environment.java:221) 82 at freemarker.core.Environment.process(Environment.java:199) 83 at freemarker.template.Template.process(Template.java:259) 84 at com.you.freemarker.FreemarkerTemplate.printFtl(FreemarkerTemplate.java:77) 85 at com.you.test.freemarker.FreemarkerTest.studentPrint(FreemarkerTest.java:217) 86 at com.you.test.freemarker.FreemarkerTest.testMyself(FreemarkerTest.java:204) 87 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 88 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 89 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 90 at java.lang.reflect.Method.invoke(Unknown Source) 91 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 92 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 93 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 94 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 95 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 96 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 97 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 98 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 99 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 100 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 101 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 102 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 103 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 104 at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 105 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 106 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 107 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 108 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 109 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 110 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 111 112 Error executing macro: write 113 required parameter: nums is not specified. 114 The problematic instruction: 115 ---------- 116 ==> macro write [on line 11, column 10 in myself.ftl] 117 in user-directive write [on line 18, column 10 in myself.ftl] 118 ---------- 119 120 Java backtrace for programmers: 121 ---------- 122 freemarker.template.TemplateException: Error executing macro: write 123 required parameter: nums is not specified. 124 at freemarker.core.Macro$Context.sanityCheck(Macro.java:211) 125 at freemarker.core.Macro$Context.runMacro(Macro.java:169) 126 at freemarker.core.Environment.visit(Environment.java:614) 127 at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106) 128 at freemarker.core.Environment.visit(Environment.java:221) 129 at freemarker.core.MixedContent.accept(MixedContent.java:92) 130 at freemarker.core.Environment.visit(Environment.java:221) 131 at freemarker.core.Environment.process(Environment.java:199) 132 at freemarker.template.Template.process(Template.java:259) 133 at com.you.freemarker.FreemarkerTemplate.printFtl(FreemarkerTemplate.java:77) 134 at com.you.test.freemarker.FreemarkerTest.studentPrint(FreemarkerTest.java:217) 135 at com.you.test.freemarker.FreemarkerTest.testMyself(FreemarkerTest.java:204) 136 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 137 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 138 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 139 at java.lang.reflect.Method.invoke(Unknown Source) 140 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 141 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 142 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 143 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 144 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 145 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 146 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 147 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 148 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 149 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 150 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 151 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 152 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 153 at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 154 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 155 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 156 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 157 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 158 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 159 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
2、错误原因
1 <#--freemarker自定义标签--> 2 <#--对于write自定义标签有两个参数,nums是次数,name是名称--> 3 <#macro write nums name> 4 <#list 1..nums as num> 5 姓名:张三丰 6 性别:男 7 </#list> 8 </#macro> 9 10 <@write/>
由于write自定义标签有两个参数,在调用它的时候需要给两个参数指定值,否则报错
3、解决办法
1 <@write nums=4 name="你好!"/>
给两个参数赋值
1 <html> 2 <head> 3 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 4 <title>freemarker自定义标签</title> 5 6 </head> 7 8 <body> 9 <#--freemarker自定义标签--> 10 <#--对于write自定义标签有两个参数,nums是次数,name是名称--> 11 <#macro write nums name> 12 <#list 1..nums as num> 13 姓名:张三丰 14 性别:男 15 </#list> 16 </#macro> 17 18 <@write nums=4 name="你好!"/> 19 </body> 20 </html>
运行结果:
1 <html> 2 <head> 3 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 4 <title>freemarker自定义标签</title> 5 6 </head> 7 8 <body> 9 10 姓名:张三丰 11 性别:男 12 姓名:张三丰 13 性别:男 14 姓名:张三丰 15 性别:男 16 姓名:张三丰 17 性别:男 18 </body> 19 </html>