Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

apache - Velocity Template engine - key-value-map

I have some problems wo use a key-value-map into Velocity.

Someone has an example of this functionality?

$myMap ={}

$myMap.put("mykey1", "myvalue")
$myMap.delete("mykey1")
$myMap.getValue("mykey1")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Nathan said, you should use:

#set ($myMap = {})

to create a new map and assign it to a variable.

Now, why is the put call printed.

  1. Anything that is not inside a directive, like #set(not printed) or #if(not printed) or #foreach(again not printed), is printed, including free text, variables, and method calls.

  2. Velocity can't distinguish between the different semantics of $myMap.get('mykey') and $myMap.put('key', 'value') (reader vs. writer), so the result of the put call is printed, just like any other method.

  3. Whenever something can't be properly evaluated, because a variable is not defined or somewhere along the line a method returns null, the code that failed to be evaluated is dumped literally into the output.

  4. As the documentation of the put method states, the function returns the previous value stored for that key, or null if no value was set at all.

  5. Summing it all up, it's normal to get that line printed.

To try this theory out, you can do this:

#set ($myMap = {})
$myMap.put('key', 'first value')
$myMap.put('key', 'second value')
$myMap.get('key')

This will be printed:

$myMap.put('key', 'first value')
first value
second value

There are two things you can do so that the line isn't printed:

  1. Store the outcome of the function in a temporary variable: #set ($discard = $myMap.put('key', 'value')

  2. Use the silent method call: $!myMap.put('key', 'value')

I'd recommend the first one, since the second one will still print something when you're replacing an existing value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...