Freemarker Hashtable <Integer,String>,按键迭代

Ibo*_*lit 4 freemarker hashtable

我有一些使用Integers作为键的哈希表,我希望能够在我的Freemarker模板中迭代它们,但是,似乎没有任何效果.

我尝试了Freemarker迭代hashmap键的例子:


<#list user.props() as prop>
   ${prop} = ${user.get(prop)}
</#list>
Run Code Online (Sandbox Code Playgroud)

它可能适用于Strings作为键,但它不适用于Integers.我甚至无法通过具体值从哈希表中检索值.我拥有的是:

Hashtalbe ht = new Hashtable();
ht.put(1, "hello");
datamodel.put("devices", ht);
Run Code Online (Sandbox Code Playgroud)

(datamodel是传递给模板的hashmap).

在模板中,我执行以下操作:


<#if devices??>
 <#list devices?keys as prop>
  <p>${prop}</p>
  <p>${devices.get(1)}</p>

OR
<p>${devices.get(key)}</p>
OR
<p>${devices[key]}</p>
OR
<p>${devices[1]}</p> </#list> <#else> <p> no devices</p> </#if>
Run Code Online (Sandbox Code Playgroud)

但这些都不起作用.你能帮我吗?

PS.我将哈希表转换为将其传递给模板,但这似乎是一种解决方法.

最好的问候,蒂莫菲

Ibo*_*lit 7

对于那些可能跟随我的脚步的人.显然,FreeMarker无法使用带有参数的Hashtables.所以我最终创建了这些哈希表inti的版本,因为我的哈希表中有数字作为键,我能够在我的模板中执行以下操作:


<#list 1..100 as prop>
    <#if hashtable[prop?string]??>
        <option value='${prop}'<#if prop==selected> selected='selected'</#if>>${hashtable[prop?string]}</option>
    <#else><#break>
    </#if>
</#list>

Run Code Online (Sandbox Code Playgroud)

祝你好运,也许力量与你同在:)

  • 实际上问题是FreeMarker只能处理类型为String的键的哈希. (2认同)