Method duplicates output

后端 未结 1 478
悲&欢浪女
悲&欢浪女 2021-01-27 21:03

Pretty new to sling and Java so I apologize in advance. But does anybody have any idea why when I\'m at the root it\'s outputting my path twice? It\'s odd that it only happens a

1条回答
  •  -上瘾入骨i
    2021-01-27 21:40

    First, ifAtRoot() will return true only if page is null because you cannot compare objects (including strings) using ==. You should use .equals() instead:

    public static boolean ifAtRoot(Page page, Page root) {
        return (page == null || root.getPath().equals(page.getPath()));
    }
    

    In your case first call of ifAtRoot() returned false, so you called it second time recursively passing brend that just has been created. The second call creates brend again and appends bc (that contains previously created brend) to it. The second call of ifAtRoot() for your luck returns true. Otherwise you'd enter infinite recursion and finish with StackOverflowError.

    0 讨论(0)
提交回复
热议问题