why this doesn't work ? please help.
czy11421 <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hi, ALL,
I try to use Freemarker to do a code generation. I am a new to this.
Here is the coding, but it doesn't work, it complains
freemarker.core.InvalidReferenceException: Expression fld.name is
undefined on line 3, column 19 in src/example.ftl.
Could you please tell me the reason ?
Here is the java code and template file.
Thanks.
Edward
////////////// code //////////
package com;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class Test {
public static void main(String[] args) {
try {
Configuration cfg = new Configuration();
Map datamodel = new HashMap();
datamodel.put("CName", "Employee");
datamodel.put("fldList", getFld());
Template tpl = cfg.getTemplate("./src/example.ftl");
OutputStreamWriter output = new OutputStreamWriter(System.out);
tpl.process(datamodel, output);
} catch (Exception e) {
e.printStackTrace();
}
}
public static ArrayList<Field> getFld() {
ArrayList<Field> list = new ArrayList<Field>();
list.add(new Field("String", "name"));
return list;
}
}
class Field {
public String name;
public String type;
public Field(){ }
public Field(String type, String name){
this.type = type;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return this.name+"--"+this.type;
}
}
////////////////// FTL template ////////////
public class ${CName}{
<#list fldList as fld>
${fld.name}
</#list>
}
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev