在Velocity中访问数组的最佳方法是什么?

时间:2022-03-18 09:23:18

I have a Java array such as:

我有一个Java数组,如:

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?

还有别的办法吗?

7 个解决方案

#1


24  

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

您可以使用Velocity 1.6:对于名为$ array的数组,可以简单地执行$ 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]).

在即将到来的Velocity 1.7中,人们将可以执行$ array [$ index](以及$ list [$ index]和$ map [$ key])。

#2


8  

You could wrap the array in a List using Arrays.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.

您可以使用Arrays.asList(T ... a)将数组包装在List中。新的List对象由原始数组支持,因此不会浪费地分配副本。即使对新列表所做的更改也会传播回阵列。

Then you can use $list.get(int index) to get your objects out in Velocity.

然后你可以使用$ list.get(int index)在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(Object array,int index)从数组中获取项。

#3


2  

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]) 

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 :)

可以通过使用$ array.get(“arr”,1)获得此值,因为没有直接的方法从数组中获取值,如$ att_id = $ arr [0]的速度。希望它有效:)

#4


1  

there is an implicit counter $velocityCount which starts with value 1 so you do not have to create your own counter.

有一个隐含的计数器$ velocityCount,它以值1开头,因此您不必创建自己的计数器。

#5


1  

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.

Brian的答案确实是正确的,尽管您可能想知道即将推出的Velocity 1.6对阵列有直接支持;有关更多信息,请参阅Velocity文档。

#6


0  

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.

我最终使用了velocity-tools.jar中的ListTool。它有方法来访问数组的元素并获得它的大小。

#7


0  

I has the same question and it got answered on another thread

我有同样的问题,并在另一个线程上得到了回答

#set ( $Page = $additionalParams.get('Page') )
#set ( $Pages = [] )
#if ( $Page != $null && $Page != "" )
    #foreach($i in $Page.split(";"))
        $Pages.add($i)
    #end
#end

Array indexing in Confluence / Velocity templates

Confluence / Velocity模板中的数组索引

#1


24  

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

您可以使用Velocity 1.6:对于名为$ array的数组,可以简单地执行$ 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]).

在即将到来的Velocity 1.7中,人们将可以执行$ array [$ index](以及$ list [$ index]和$ map [$ key])。

#2


8  

You could wrap the array in a List using Arrays.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.

您可以使用Arrays.asList(T ... a)将数组包装在List中。新的List对象由原始数组支持,因此不会浪费地分配副本。即使对新列表所做的更改也会传播回阵列。

Then you can use $list.get(int index) to get your objects out in Velocity.

然后你可以使用$ list.get(int index)在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(Object array,int index)从数组中获取项。

#3


2  

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]) 

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 :)

可以通过使用$ array.get(“arr”,1)获得此值,因为没有直接的方法从数组中获取值,如$ att_id = $ arr [0]的速度。希望它有效:)

#4


1  

there is an implicit counter $velocityCount which starts with value 1 so you do not have to create your own counter.

有一个隐含的计数器$ velocityCount,它以值1开头,因此您不必创建自己的计数器。

#5


1  

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.

Brian的答案确实是正确的,尽管您可能想知道即将推出的Velocity 1.6对阵列有直接支持;有关更多信息,请参阅Velocity文档。

#6


0  

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.

我最终使用了velocity-tools.jar中的ListTool。它有方法来访问数组的元素并获得它的大小。

#7


0  

I has the same question and it got answered on another thread

我有同样的问题,并在另一个线程上得到了回答

#set ( $Page = $additionalParams.get('Page') )
#set ( $Pages = [] )
#if ( $Page != $null && $Page != "" )
    #foreach($i in $Page.split(";"))
        $Pages.add($i)
    #end
#end

Array indexing in Confluence / Velocity templates

Confluence / Velocity模板中的数组索引