在 Velocity 中访问数组的最佳方法是什么? [英] What is the best way to access an array inside Velocity?

查看:34
本文介绍了在 Velocity 中访问数组的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 数组,例如:

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

还有什么办法吗?

推荐答案

您可以使用 Velocity 1.6:对于名为 $array 的数组,只需执行 $array.get($索引).

You can use use Velocity 1.6: for an array named $array one can simply do $array.get($index).

在即将发布的 Velocity 1.7 中,您将能够执行 $array[$index](以及 $list[$index]$地图[$key]).

In the upcoming Velocity 1.7, one will be able to do $array[$index] (as well as $list[$index] and $map[$key]).

这篇关于在 Velocity 中访问数组的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆