velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <...@intermeta.de>
Subject Re: Putting key/value pair into hashmap from velocity template
Date Wed, 19 Jul 2006 20:53:50 GMT
"Matthias Hendler" <hendler2@gmx.de> writes:

>Hello,

Hi,


>I try to put a key/value pair to a hashmap from velocity template.

>First I put a new hashmap object into velocity context named "myMap".
>In my templates I can write:
>...
>$myMap.clear()
>Size: $myMap.size()
>...
>The above works fine. Now I want to put a key/value pair into the hashmap using the method
put(object, object).

>So I wrote:
>...
>${myMap.put(["123", "test"])}
>...
>And I get an exception: "${myMap.put(["123", "test"])} is not a valid reference."

>What's wrong?

Ok, let me start (all of the following examples use the current
Velocity 1.5-dev release. This _should_ be the same for 1.4 but there
might be a few subtle differences. YMMV):

Your method call is plain wrong. :-) What you are doing is calling
java.util.Map::put() with a List as its only parameter. However
java.util.Map::put takes a key and a value. Velocity converts ["123",
"test"] to a List containing two parameters and then tries to find a
put method on your myMap object which takes just one parameter. That
does not exist and you get an error (and the line does nothing. :-) )

As some people already suggested, you must write

${myMap.put("123", "test")}


Now you might wonder why Velocity might echo this line (though it
stores the value in the map). Because you cleared the map before
putting a value in, the current value of the key "123" is null. When
executing ${myMap.put}, you are requesting that the output of this
method call is rendered to the template. java.util.Map::put returns
the old value of the key (before storing the new value).  If this
value is null, then Velocity simply echoes the reference itself. (you
can turn this behaviour off in the current 1.5 snapshot by setting a
property whose name I've currently forgotten. ;-) ).

So you should write $!{myMap.put("123", "test")} (use $! for quiet
notation).

If you plan on using static keys (because you are e.g. filling up a
map with constants _and_ if your constants start with a letter (not a
number as in your example), you can also write

#set ($myMap.abc = "test")

which is equal to calling ${myMap.put("abc", "test")}. However,
Velocity requires that the property does not start with a digit.

	Best regards
		Henning

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

Social behaviour: Bavarians can be extremely egalitarian and folksy.
                                    -- http://en.wikipedia.org/wiki/Bavaria
Most Franconians do not like to be called Bavarians.
                                    -- http://en.wikipedia.org/wiki/Franconia

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message