Freemarker无法从列表中获取对象值

时间:2015-12-29 03:18:24

标签: java freemarker

我使用最新的freemarker版本2.3.23 我有包含User对象的列表,User对象很简单,只包含name属性。我的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)

2 个答案:

答案 0 :(得分:0)

尝试按如下方式替换您的ftl文件。

<#list userlist as item>
    ${item.name}
</#list>

希望这可以解决您的问题。祝你有愉快的一天: - )

答案 1 :(得分:0)

只公开公共类的方法/属性,因此User类必须是公共的(或getName()必须从公共类/接口继承。)