Freemarker错误

 

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 error
SEVERE: 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)

1个回答

[code="java"]
t.process(t, new OutputStreamWriter(System.out));

[/code]
你这里搞错了,没有把参数传进去,改成这样:
[code="java"]
t.process(root, new OutputStreamWriter(System.out));

[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问