在velocity中,可以遍历java hashmap的条目集()吗?

时间:2022-11-24 18:57:41

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?

我做错了什么?

4 个解决方案

#1


91  

Your mistake is referring to key and value as 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()。

#2


4  

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

我正在寻找一种方法来循环遍历一个HashMap in velocity,这个方法也可以。

#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那样。

#3


0  

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.

在上面的代码中,我们可以看到check值将类似于“data1、data2等…”,但是在使用get()之后,我们可以获得实例值。

#4


-1  

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。

#1


91  

Your mistake is referring to key and value as 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()。

#2


4  

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

我正在寻找一种方法来循环遍历一个HashMap in velocity,这个方法也可以。

#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那样。

#3


0  

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.

在上面的代码中,我们可以看到check值将类似于“data1、data2等…”,但是在使用get()之后,我们可以获得实例值。

#4


-1  

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。