Freemarker 无法从列表中获取对象值
Freemarker cannot get object value from the list
我用的是最新的freemarker 2.3.23版本
我有一个包含用户对象的列表,用户对象很简单,只包含名称 属性。我的 ftl 文件如下所示:
<#list userlist>
<#items as item>
${item.name}
</#items>
我的 Java 代码如下所示:
public static void main(String[] args) throws Exception {
List<User> user = new ArrayList<User>();
User user1 = new User();
User user2 = new User();
user1.setName("jason");
user2.setName("tony");
user.add(user1);
user.add(user2);
Map<String, Object> params = new HashMap<String, Object>();
params.put("userlist", user);
Template template = FreemarkerUtil.getTemplate(FreemarkerUtil.TEMPLATE_PATH, "test.ftl");
FileWriter fw = null;
try {
fw = new FileWriter(new File(FreemarkerUtil.getProjOutputDomainPath() + "test.txt"));
template.process(params, fw);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fw != null) {
fw.close();
}
}
}
class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
错误如下所示:
The following has evaluated to null or missing:
==> item.name [in template "test.ftl" at line 3, column 19]`enter code here`
Tip: It's the step after the last dot that caused this error, not those before it.
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
FTL stack trace ("~" means nesting-related):
- Failed at: ${item.name} [in template "test.ftl" at line 3, column 17]
Java stack trace (for programmers):
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131)
at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:355)
at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
at freemarker.core.DollarVariable.accept(DollarVariable.java:41)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:268)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
at freemarker.core.IteratorBlock$IterationContext.loopForItemsElement(IteratorBlock.java:207)
at freemarker.core.Items.accept(Items.java:43)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:276)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194)
at freemarker.core.Environment.visitIteratorBlock(Environment.java:572)
at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.Environment.process(Environment.java:302)
at freemarker.template.Template.process(Template.java:325)
at auto.freemarker.template.MySQLGenerator.main(MySQLGenerator.java:61)
尝试按如下方式替换您的 ftl 文件。
<#list userlist as item>
${item.name}
</#list>
希望这能解决您的问题。祝你有美好的一天:-)
只有publicclass的methods/properties暴露了,所以User
class必须是public(或者getName()
必须继承自 public class/interface).
我用的是最新的freemarker 2.3.23版本 我有一个包含用户对象的列表,用户对象很简单,只包含名称 属性。我的 ftl 文件如下所示:
<#list userlist>
<#items as item>
${item.name}
</#items>
我的 Java 代码如下所示:
public static void main(String[] args) throws Exception {
List<User> user = new ArrayList<User>();
User user1 = new User();
User user2 = new User();
user1.setName("jason");
user2.setName("tony");
user.add(user1);
user.add(user2);
Map<String, Object> params = new HashMap<String, Object>();
params.put("userlist", user);
Template template = FreemarkerUtil.getTemplate(FreemarkerUtil.TEMPLATE_PATH, "test.ftl");
FileWriter fw = null;
try {
fw = new FileWriter(new File(FreemarkerUtil.getProjOutputDomainPath() + "test.txt"));
template.process(params, fw);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fw != null) {
fw.close();
}
}
}
class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
错误如下所示:
The following has evaluated to null or missing:
==> item.name [in template "test.ftl" at line 3, column 19]`enter code here`
Tip: It's the step after the last dot that caused this error, not those before it.
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
FTL stack trace ("~" means nesting-related):
- Failed at: ${item.name} [in template "test.ftl" at line 3, column 17]
Java stack trace (for programmers):
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131)
at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:355)
at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
at freemarker.core.DollarVariable.accept(DollarVariable.java:41)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:268)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
at freemarker.core.IteratorBlock$IterationContext.loopForItemsElement(IteratorBlock.java:207)
at freemarker.core.Items.accept(Items.java:43)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:276)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194)
at freemarker.core.Environment.visitIteratorBlock(Environment.java:572)
at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.Environment.process(Environment.java:302)
at freemarker.template.Template.process(Template.java:325)
at auto.freemarker.template.MySQLGenerator.main(MySQLGenerator.java:61)
尝试按如下方式替换您的 ftl 文件。
<#list userlist as item>
${item.name}
</#list>
希望这能解决您的问题。祝你有美好的一天:-)
只有publicclass的methods/properties暴露了,所以User
class必须是public(或者getName()
必须继承自 public class/interface).