速度,如何使用另一个速度变量检索哈希图值 [英] Velocity, how to retrieve a hashmap value using another velocity variable
问题描述
我的 bean 中有一个 HashMap:
I have a HashMap in my bean:
HashMap<String, SomeObject> someHashMap;
然后在速度模板中,我需要使用我从其他来源获得的速度值访问 HashMap(实际上我有很多键,而不仅仅是一个,这就是我需要以这种方式获取值的原因):
Then in the velocity template I need to access the HashMap with a value that I have in velocity from other source (in fact I have many keys not only one that's why I need to get the values this way):
$key
如何使用此键访问哈希图?我正在尝试:
How can I access the hashmap with this key? I'm trying:
$someHashMap.get($key)
和
${someHashMap.get($key)}
但是这两个只向输出写入相同的内容,这意味着我从字面上得到了第一行:
But those two only write the same thing to the output, meaning that with the first line I literally get:
$someHashMap.get($key)
在网页中.
执行此操作的正确方法/语法是什么?
Which is the correct way/syntaxis to do this?
谢谢!
推荐答案
我遇到了完全相同的问题.就我而言,我尝试这样做:
I had this exact same problem. In my case I tried to do this:
$map.get($locale)
其中 $locale
是例如fi_FI"
.我通过在括号内添加引号来解决它:
where $locale
is e.g. "fi_FI"
. I solved it by adding quotes inside the brackets:
$map.get("$locale")
我不确定,但我认为基本原理是这样的:
I'm not sure, but I think the rationale goes like this:
$map.get( $locale ) -> $map.get( fi_FI ) -> Velocity gets confused
$map.get("$locale") -> $map.get("fi_FI") -> Velocity retrieves correct value
这篇关于速度,如何使用另一个速度变量检索哈希图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!