Freemarker Hashtable,按键迭代
我有许多以整数作为键的哈希表,我希望能够在我的 Freemarker 模板中迭代它们,但是,似乎没有任何效果。
我尝试了 Freemarker 迭代哈希映射键中的示例:
<#list user.props() as prop>
${prop} = ${user.get(prop)}
</#list>
它可能适用于字符串作为键,但它不适用于整数。我什至无法通过具体值从哈希表中检索值。我所拥有的是:(
Hashtalbe ht = new Hashtable();
ht.put(1, "hello");
datamodel.put("devices", ht);
数据模型是传递给模板的哈希图)。
在模板中,我执行以下操作:
<#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>
但这些都不起作用。你能帮我吗?
附言。我将哈希表从 转换为将其传递给模板,但这似乎是一种解决方法。
此致, 蒂莫菲
I have a number of hashtables with Integers as keys, and i want to be able to iterate over them in my Freemarker templates, however, nothing seems to work.
I tried the example from Freemarker iterating over hashmap keys:
<#list user.props() as prop>
${prop} = ${user.get(prop)}
</#list>
It probably works with Strings as keys, but it didn't with Integers. I also can't even retrieve the value from my hashtable by the concrete value. What i have is:
Hashtalbe ht = new Hashtable();
ht.put(1, "hello");
datamodel.put("devices", ht);
(datamodel being the hashmap passed to the template).
In the template i do the following:
<#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>
But none of that works. Can you help me, please?
PS. I converted the hashtable from into to pass it to the template, but that seems like a bit of a workaround.
Best regards,
Timofey
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
献给那些追随我脚步的人。
显然,FreeMarker 无法使用带有 as 参数的哈希表。因此,我最终创建了这些哈希表 inti 的版本,并且由于我的哈希表中以数字作为键,因此我能够在模板中执行以下操作:
祝你好运,愿力量与你同在:)
for those of you who may follow my footsteps.
Apparently, FreeMarker can't work with Hashtables with as parameters. So I ended up creating the versions of these hashtables inti and, since i had numbers as keys in my hashtables, i was able to do the following in my template:
Good Luck and may the force be with you :)
这是老问题了,FTL 的 hash 类型不像 Java 的 Map,它 仅支持字符串键。但是,从 2.3.22 开始,您可以使用
someMap?api.get(someNonStringKey)
来解决这个问题。它需要一些配置才能启用,但不会破坏现有应用程序。请参阅此答案或此常见问题解答条目。This is the old issue that FTL's hash type isn't like Java's
Map
, and it only supports string keys. But, since 2.3.22 you can usesomeMap?api.get(someNonStringKey)
to work that around. It needs some configuring to enable, but nothing that breaks an existing application. See this answer or this FAQ entry.