在 Velocity 中访问数组的最佳方式是什么?
我有一个 Java 数组,例如:
String[] arr = new String[] {"123","doc","projectReport.doc"};
在我看来,访问的自然方式是:
#set($att_id = $arr[0])
#set($att_type = $arr[1])
#set($att_name = $arr[2])
但它不起作用。 我已经提出了这个解决方法。 但对于这样一个简单的任务来说,代码有点太多了。
#set($counter = 0)
#foreach($el in $arr)
#if($counter==0)
#set($att_id = $el)
#elseif($counter==1)
#set($att_type = $el)
#elseif($counter==2)
#set($att_name = $el)
#end
#set($counter = $counter + 1)
#end
还有其他办法吗?
I have a Java array such as:
String[] arr = new String[] {"123","doc","projectReport.doc"};
In my opinion the natural way to access would be:
#set($att_id = $arr[0])
#set($att_type = $arr[1])
#set($att_name = $arr[2])
But that it is not working. I have come with this workaround. But it a bit too much code for such an easy task.
#set($counter = 0)
#foreach($el in $arr)
#if($counter==0)
#set($att_id = $el)
#elseif($counter==1)
#set($att_type = $el)
#elseif($counter==2)
#set($att_name = $el)
#end
#set($counter = $counter + 1)
#end
Is there any other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 Velocity 1.6:对于名为
$array
的数组,只需执行$array.get($index)
即可。在即将推出的 Velocity 1.7 中,人们将能够执行
$array[$index]
(以及$list[$index]
和$map[$键]
)。You can use use Velocity 1.6: for an array named
$array
one can simply do$array.get($index)
.In the upcoming Velocity 1.7, one will be able to do
$array[$index]
(as well as$list[$index]
and$map[$key]
).您可以将数组包装在
使用
Arrays.asList(T... a)
。 新的 List 对象由原始数组支持,因此不会浪费地分配副本。 即使对新列表所做的更改也会传播回数组。然后您可以使用
$list.get(int index)
在 Velocity 中获取对象。如果您只需要从数组中获取一两个对象,还可以使用
Array.get(对象数组,int索引)
从数组中获取一个项目。
You could wrap the array in a
List
usingArrays.asList(T... a)
. The new List object is backed by the original array so it doesn't wastefully allocate a copy. Even changes made to the new List will propagate back to the array.Then you can use
$list.get(int index)
to get your objects out in Velocity.If you need to get just one or two objects from an array, you can also use
Array.get(Object array, int index)
to get an item from an array.
在我看来,自然的访问方式是:
可以使用
$array.get("arr", 1)
获取该值,因为没有直接的方法从数组获取值就像速度中的$att_id = $arr[0]
一样。希望它有效:)
In my opinion the natural way to access would be:
The value for this can be get by using
$array.get("arr", 1)
because there is no direct way to get the value from array like$att_id = $arr[0]
in velocity.Hope it works :)
Velocity 1.6
http://velocity.apache.org /engine/1.7/user-guide.html
Velocity 1.6
http://velocity.apache.org/engine/1.7/user-guide.html
有一个隐式计数器 $velocityCount 从值 1 开始,因此您不必创建自己的计数器。
there is an implicit counter $velocityCount which starts with value 1 so you do not have to create your own counter.
布莱恩的答案确实是正确,尽管您可能想知道即将推出的 Velocity 1.6 直接支持数组; 请参阅 Velocity 文档 了解更多信息。
Brian's answer is indeed correct, although you might like to know that upcoming Velocity 1.6 has direct support for arrays; see the Velocity documentation for more information.
我最终使用了 ListTool 来自velocity-tools.jar。 它具有访问数组元素并获取其大小的方法。
I ended up using the ListTool from the velocity-tools.jar. It has methods to access an array's elements and also get its size.
我有同样的问题,并在另一个线程上得到了回答
Confluence / Velocity 模板中的数组索引
I has the same question and it got answered on another thread
Array indexing in Confluence / Velocity templates