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

发布于 2024-12-25 07:24:02 字数 382 浏览 5 评论 0原文

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

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

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

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

我已经尝试过:

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

我在 JIRA 中使用 Velocity,而 JIRA 恰好使用 Velocity 版本 1.4,该版本显然不支持访问上面尝试过的数组。

非常感谢任何帮助。

I am using Velocity Templating Language and currently have:

#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?

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.

Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

携君以终年 2025-01-01 07:24:02

在 Velocity 1.6 中测试。

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

Tested in Velocity 1.6.

#foreach ($element in $string.split(";"))
   $element
#end
慈悲佛祖 2025-01-01 07:24:02

当我使用 Arrays.asList() 将数组转换为列表,然后使用列表中的方法访问元素时,它就起作用了。

我将以下内容添加到上下文中:

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

在我使用的速度模板中:

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

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

使用如下字符串数组,

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

我得到了预期的输出:

test1

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

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

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

I get the expected output:

test1
平安喜乐 2025-01-01 07:24:02

从 Velocity 1.6 开始,所有数组引用现在都被“神奇地”处理为固定长度列表。这意味着您可以对数组引用调用 java.util.List 方法。因此,如果您有对数组的引用(假设这是一个具有三个值的 String[]),您可以执行以下操作:

$myarray.isEmpty()

$myarray.size()

$myarray.get(2) 

$myarray.set(1, '测试')

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

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:

$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

数理化全能战士 2025-01-01 07:24:02

也可以像这样将元素推入数组中。

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

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

#set ($matchingProducts = [])
#set($bar = $matchingProducts.add($p))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文