Freemarker 无法从列表中获取对象值
Posted
技术标签:
【中文标题】Freemarker 无法从列表中获取对象值【英文标题】:Freemarker cannot get object value from the list 【发布时间】:2016-04-02 21:59:50 【问题描述】:我使用最新的 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)
【问题讨论】:
【参考方案1】:仅公开公共类的方法/属性,因此User
类必须是公共的(或者getName()
必须继承自公共类/接口)。
【讨论】:
我已经将用户类解压为公共类,现在可以了。谢谢。 嵌套类也可以是公共的,所以不需要提取。【参考方案2】:尝试如下替换您的 ftl 文件。
<#list userlist as item>
$item.name
</#list>
希望这能解决您的问题。祝你有美好的一天:-)
【讨论】:
以上是关于Freemarker 无法从列表中获取对象值的主要内容,如果未能解决你的问题,请参考以下文章
在 Freemarker 模板中,如何获取数据模型类的名称?
Angular 6 无法从提供的对象中自动选择/绑定下拉列表值