【问题标题】:How get first element of string array in jira velocity?如何在jira速度中获取字符串数组的第一个元素?
【发布时间】:2020-02-12 15:14:55
【问题描述】:

我是速度新手,需要帮助。 我有 com.atlassian.jira.user.RemoteUser 和 remoteUser.getDisplayName() 返回字符串,我想围绕给定正则表达式的匹配项进行拆分。然后得到字符串数组的第一个元素,但不幸的是我有:

#set ($userName = $remoteUser.getDisplayName().split(" "))

$userName, Hello!

并且有 [Ljava.lang.String;@74a70acb 因为我可以理解它是字符串数组。

但如果我尝试获取
#set ($userName = $remoteUser.getDisplayName().split(" ")[0])

$userName, Hello!

我有

org.apache.velocity.exception.ParseErrorException: Encountered "[" at getEncodedBodyFromContent[line 1, column 58] 期待以下之一: ... ... “——”…… “+” ... “*” ... “/” ... “%” ... ... ... ... ... ... ... ... ... ...

如果我尝试#set ($userName = $remoteUser.getDisplayName().split(" ")).get(0)),则会出现空指针异常

【问题讨论】:

    标签: jira velocity


    【解决方案1】:

    Velocity 1.6.4 已经十多年了,还不支持数组。

    Velocity 1.7 于 2010 年 11 月发布,支持数组。

    某个地方的某个人应该升级某些东西(仅供参考,当前版本是 Velocity 2.2 ...)。

    【讨论】:

    • 对不起,我的问题是错字。实际上我在表达式中有足够的大括号,所以我不明白为什么我有 org.apache.velocity.exception.ParseErrorException: Encountered "[" at getEncodedBodyFromContent[line 1, column 58]
    • @Duxa 您使用的是哪个版本的 Velocity?就这简单的一行,我对 1.7 或 2.2 没有任何解析问题。
    • 我找到velocity-1.6.4-atlassian-7.jar
    • 这里是同样的问题,没有决定stackoverflow.com/questions/11776820/…
    【解决方案2】:

    我不知道为什么,但是$string.split(" ") 命令不起作用。之后我以为我有字符串数组,但在$userName = $remoteUser.getDisplayName().split(" ").get($index) 之后我有空指针异常。
    有帮助的解决方案是使用 indexOf()substring() 这样的命令:

    #set ($userName = $remoteUser.getDisplayName())
    #set ($index = $userName.indexOf(' '))
    #set ($surname = $userName.substring(0, $index))
    #set ($index = $index + 1)
    #set ($name = $userName.substring($index))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-07
      • 2018-12-31
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多