在Velocity中使用string.split后如何访问数组元素?

时间:2022-04-07 21:37:57

I am using Velocity Templating Language and currently have:

我正在使用Velocity模板语言,目前有:

#set ( $stringList = $string.split(",") )

which works fine and splits the string up using a ',' as a delimiter as expected.

工作正常,并使用','作为分隔符按预期分割字符串。

My question is how do i now access each of the elements in the $stringList?

我的问题是我现在如何访问$ stringList中的每个元素?

I have tried:

我努力了:

$stringList.get(0)
$stringList[0]
$stringList.[0]
${stringList}.get(0)

I am using Velocity in JIRA and JIRA happens to use Velocity version 1.4 which apparently doesn't have support for accessing arrays as tried above.

我在JIRA中使用Velocity,而JIRA恰好使用Velocity 1.4版,它显然不支持访问数组,如上所述。

Any help is much appreciated.

任何帮助深表感谢。

4 个解决方案

#1


1  

It works when I convert the array to a List using Arrays.asList() and then use methods from List to access elements.

当我使用Arrays.asList()将数组转换为List然后使用List中的方法来访问元素时,它可以工作。

I add the following to the context:

我在上下文中添加以下内容:

context.put("arrays", Arrays.class);

In velocity template I use:

在速度模板中,我使用:

#set ( $array = $getarray.getArray() )

$arrays.asList($array).get(0)

With a String-Array as follows

使用String-Array如下

new String[] {"test1", "test2", "test3", "test4"};

I get the expected output:

我得到了预期的输出:

test1

#2


6  

Tested in Velocity 1.6.

测试速度1.6。

#foreach ($element in $string.split(";"))
   $element
#end

#3


1  

As of Velocity 1.6, all array references are now "magically" treated as if they are fixed-length lists. This means that you can call java.util.List methods on array references. So, if you have a reference to an array (let's say this one is a String[] with three values), you can do:

从Velocity 1.6开始,所有数组引用现在都被“神奇地”处理,就像它们是固定长度列表一样。这意味着您可以在数组引用上调用java.util.List方法。所以,如果你有一个数组的引用(假设这个是一个带有三个值的String []),你可以这样做:

$myarray.isEmpty()

$myarray.size()

$myarray.get(2) 

$myarray.set(1, 'test')

Source: http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html#methods

#4


0  

Its also possible to push elements into an array like this.

它也可以将元素推送到这样的数组中。

#set ($matchingProducts = [])
#set($bar = $matchingProducts.add($p))

#1


1  

It works when I convert the array to a List using Arrays.asList() and then use methods from List to access elements.

当我使用Arrays.asList()将数组转换为List然后使用List中的方法来访问元素时,它可以工作。

I add the following to the context:

我在上下文中添加以下内容:

context.put("arrays", Arrays.class);

In velocity template I use:

在速度模板中,我使用:

#set ( $array = $getarray.getArray() )

$arrays.asList($array).get(0)

With a String-Array as follows

使用String-Array如下

new String[] {"test1", "test2", "test3", "test4"};

I get the expected output:

我得到了预期的输出:

test1

#2


6  

Tested in Velocity 1.6.

测试速度1.6。

#foreach ($element in $string.split(";"))
   $element
#end

#3


1  

As of Velocity 1.6, all array references are now "magically" treated as if they are fixed-length lists. This means that you can call java.util.List methods on array references. So, if you have a reference to an array (let's say this one is a String[] with three values), you can do:

从Velocity 1.6开始,所有数组引用现在都被“神奇地”处理,就像它们是固定长度列表一样。这意味着您可以在数组引用上调用java.util.List方法。所以,如果你有一个数组的引用(假设这个是一个带有三个值的String []),你可以这样做:

$myarray.isEmpty()

$myarray.size()

$myarray.get(2) 

$myarray.set(1, 'test')

Source: http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html#methods

#4


0  

Its also possible to push elements into an array like this.

它也可以将元素推送到这样的数组中。

#set ($matchingProducts = [])
#set($bar = $matchingProducts.add($p))