62299

Method duplicates output

Question:

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 at the absolute root.

public static String generateTest(Page page, Page rootPage, String bc) { Page parent = page.getParent(); String bread = ""; bread += (parent != null) ? "<li><a href=" + parent.getPath() + ">" + parent.getTitle() + "</a>" : ""; bread += "<li>" + "<a href=" + page.getPath() + ">" + page.getTitle() + "</a></li>" + bc; return (ifAtRoot(parent , rootPage)) ? breadcrumb : generateTest(parent, rootPage, bread); } public static boolean ifAtRoot(Page page, Page root) { return (page == null || root.getPath() == page.getPath()); }

Any help is greatly appreciated!

Answer1:

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.

Recommend

  • ionic2 angular2 add graph component (with its own selector) to a page
  • How to navigate Page without Top Tabs App.components root
  • Mapping with Hibernate Annotations in Grails 3.0.1
  • Bread-crumb style navigation for Winforms
  • How to make string to dictionary and update values that exist?
  • How to use Spring Batch read CSV,process it and write it as a CSV with one row can produce more than
  • Skip items in recycler view
  • RecyclerView items are not painted properly
  • Creating dummy variables from multiple strings in the same row
  • Android theme not affecting TextViews inside list item
  • Android textview stackoverflow when using Hebrew
  • Navigation Drawer list items hide/go invisible on scroll
  • Session Variable not working in Internet Explorer, but works fine in Firefox / Chrome
  • Jest ElasticSearch Search API Hits mapping
  • Copying Images between Google Form quizzes
  • Delete row dynamically from table layout in android
  • Change background of Expandable List child view when an element in child layout is clicked
  • JSoup not showing correct text
  • Help refreshing list with notifyDataSetChanged()
  • Angular: Binding to a observable property of service -> value doesn't update
  • R grep regular expression using elements in a vector
  • How to switch between tabs in Android?
  • Getting States and Provinces in .NET
  • Drag and Drop for imageview not working
  • Put database into android project
  • Is it possible to run clang with llc flags
  • Force show.bind execution
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Disable Enter in editText android
  • Highlight one bar in a series in highcharts?
  • DomPDF {PAGE_NUM} not on first page
  • javascript inside java/jsp code
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • A cron job substitute?
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • How can i traverse a binary tree from right to left in java?