问题描述
public class HelloFreeMarker {private Configuration cfg ;public void init() throws Exception {cfg = new Configuration();cfg.setDirectoryForTemplateLoading(new File("src\com\s2freemarker\exer\templates"));}public void process() throws Exception{Map<Object,Object> root = new HashMap<Object,Object>();root.put("name", "FreeMarker");root.put("msg", "First example for freemarker .");Template t = cfg.getTemplate("test.ftl");t.process(t, new OutputStreamWriter(System.out));}/** * @param args */public static void main(String[] args) throws Exception{HelloFreeMarker hfm = new HelloFreeMarker();hfm.init();hfm.process();}} 模版文件: ${name},hello!${msg} 错误提示: Aug 18, 2011 10:49:25 AM freemarker.log.JDK14LoggerFactory$JDK14Logger errorSEVERE: Expression msg is undefined on line 1, column 17 in test.ftl.Expression msg is undefined on line 1, column 17 in test.ftl.The problematic instruction:----------==> ${msg} [on line 1, column 15 in test.ftl]----------Java backtrace for programmers:----------freemarker.core.InvalidReferenceException: Expression msg is undefined on line 1, column 17 in test.ftl.at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)at freemarker.core.Expression.getStringValue(Expression.java:118)at freemarker.core.Expression.getStringValue(Expression.java:93)at freemarker.core.DollarVariable.accept(DollarVariable.java:76)at freemarker.core.Environment.visit(Environment.java:210)at freemarker.core.MixedContent.accept(MixedContent.java:92)at freemarker.core.Environment.visit(Environment.java:210)at freemarker.core.Environment.process(Environment.java:190)at freemarker.template.Template.process(Template.java:237)at com.s2freemarker.exer.test.HelloFreeMarker.process(HelloFreeMarker.java:30)at com.s2freemarker.exer.test.HelloFreeMarker.main(HelloFreeMarker.java:39)test.ftl,hello!Expression msg is undefined on line 1, column 17 in test.ftl.The problematic instruction:----------==> ${msg} [on line 1, column 15 in test.ftl]----------Java backtrace for programmers:----------freemarker.core.InvalidReferenceException: Expression msg is undefined on line 1, column 17 in test.ftl.at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)at freemarker.core.Expression.getStringValue(Expression.java:118)at freemarker.core.Expression.getStringValue(Expression.java:93)at freemarker.core.DollarVariable.accept(DollarVariable.java:76)at freemarker.core.Environment.visit(Environment.java:210)at freemarker.core.MixedContent.accept(MixedContent.java:92)at freemarker.core.Environment.visit(Environment.java:210)at freemarker.core.Environment.process(Environment.java:190)at freemarker.template.Template.process(Template.java:237)at com.s2freemarker.exer.test.HelloFreeMarker.process(HelloFreeMarker.java:30)at com.s2freemarker.exer.test.HelloFreeMarker.main(HelloFreeMarker.java:39)Exception in thread "main" Expression msg is undefined on line 1, column 17 in test.ftl.The problematic instruction:----------==> ${msg} [on line 1, column 15 in test.ftl]----------Java backtrace for programmers:----------freemarker.core.InvalidReferenceException: Expression msg is undefined on line 1, column 17 in test.ftl.at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)at freemarker.core.Expression.getStringValue(Expression.java:118)at freemarker.core.Expression.getStringValue(Expression.java:93)at freemarker.core.DollarVariable.accept(DollarVariable.java:76)at freemarker.core.Environment.visit(Environment.java:210)at freemarker.core.MixedContent.accept(MixedContent.java:92)at freemarker.core.Environment.visit(Environment.java:210)at freemarker.core.Environment.process(Environment.java:190)at freemarker.template.Template.process(Template.java:237)at com.s2freemarker.exer.test.HelloFreeMarker.process(HelloFreeMarker.java:30)at com.s2freemarker.exer.test.HelloFreeMarker.main(HelloFreeMarker.java:39)
解决方案
t.process(t, new OutputStreamWriter(System.out)); 你这里搞错了,没有把参数传进去,改成这样:t.process(root, new OutputStreamWriter(System.out));