TheJuniorProgrammer TheJuniorProgrammer - 3 months ago 12
Java Question

Freemarker + Java - How to "lookup" a value using key

In Java, to look up a value using the key, I would use the

.get
method:

String value = hashmap.get(key);


In Freemarker, apparently
.get
is deprecated or something, because when I try to use it:

<#list hash?keys as key>
<p>${hash.get(key)}</p>
</#list>


it returns a null exception:

freemarker.core.InvalidReferenceException: The following has evaluated to null or missing

----
FTL stack trace ("~" means nesting-related):
- Failed at: ${hash.get(key)} [in template "___.ftl" at line ___, column ___]
----

Answer
<#list hash?keys as key>
    <p>${hash[key]}</p>
</#list>

See Retrieving data from a hash section in the Freemarker documentation.

Comments