Jason Yau
Jason Yau

Reputation: 13

Freemarker cannot get object value from the list

I use the latest freemarker version 2.3.23 I have list which contains the User object, User object is simple which only contains the name property. My ftl file show like below:

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

My Java Code show like below:

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;
    }

}

Error shows below:

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)

Upvotes: 1

Views: 10522

Answers (2)

ddekany
ddekany

Reputation: 31112

Only the methods/properties of public classes are exposed, thus the User class must be public (or getName() must be inherited from a public class/interface).

Upvotes: 2

Niru
Niru

Reputation: 489

Try to replace your ftl file as follows.

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

Hope this should fix your issue. Have a nice day :-)

Upvotes: 0

Related Questions