在速度中,您可以遍历 java hashmap 的条目 set() 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2049981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:00:35  来源:igfitidea点击:

in velocity can you iterate through a java hashmap's entry set()?

javaxmlvelocitytemplate-engine

提问by Ayrad

Can you do something like this in a velocity template?

你能在速度模板中做这样的事情吗?

#set ($map = $myobject.getMap() )
#foreach ($mapEntry in $map.entrySet())
    <name>$mapEntry.key()</name>
    <value>$mapEntry.value()</value>
#end

it outputs blank tags like so:

它像这样输出空白标签:

<name></name> 

and

<value></value> 

What am I doing wrong?

我究竟做错了什么?

采纳答案by Yoni

Your mistake is referring to keyand valueas methods (with trailing "()" parenthesis) instead of as properties. Try this:

您的错误是将称为方法(带有尾随的“()”括号)而不是属性。尝试这个:

#set ($map = $myobject.getMap() )
#foreach ($mapEntry in $map.entrySet())
    <name>$mapEntry.key</name>
    <value>$mapEntry.value</value>
#end

In other words, use either a property, like mapEntry.key, or the method, like mapEntry.getKey().

换句话说,使用属性(如mapEntry.key)或方法(如mapEntry.getKey() )

回答by Allan Ruin

I'm looking for a way to loop through a HashMap in velocity, and this will work too.

我正在寻找一种以速度循环遍历 HashMap 的方法,这也可以。

#set ($map = $myobject.getMap())
#foreach( $key in $map.keySet())
      <name>$key</name>
      <value>$resume.get($key)</value>
#end

Just like the way you would loop through a HashMap in java.

就像在 Java 中循环遍历 HashMap 的方式一样。

回答by Vincent Gerris

To clarify (I cannot comment), in general you can use either the Java get methods, or replace them by the corresponding name without with a small letter and without ().

为了澄清(我无法发表评论),通常您可以使用 Java get 方法,或者用相应的名称替换它们,而不用小写字母和().

So $mapEntry.getKey()or map.key.

所以$mapEntry.getKey()map.key

回答by user7490061

Here the Value

这里的价值

itemsValue={data1=1,data2=2,data3=3}

So , we need to iterate the group of value;

所以,我们需要迭代一组值;

foreach ($key in ${itemsValue.keySet()})
   if($itemsValue.get($key)==1)
        Condition
   end
end

In the above code we can see check the value will be like -"data1,data2 etc ..." but after using the get(), we can able to get the instance value.

在上面的代码中,我们可以看到检查值将类似于 -"data1,data2 etc ..." 但是在使用 get() 之后,我们可以获取实例值。