简体   繁体   English

Apache Sling循环重复

[英]Apache Sling loop repeating

Does anyone know why the code below is outputting 2 of each navigation element. 有谁知道为什么下面的代码输出每个导航元素2个。

//get the full path to the current page
String home = Text.getAbsoluteParent(currentPage.getPath(), 2);    
int absParent = currentStyle.get("absParent", 1);

//checks for invalid and hidden pages.
PageFilter filter = new PageFilter(request);

//utility class that provides an iterator over navigation elements

Navigation nav = new Navigation(currentPage, absParent, filter, 1);

for (Navigation.Element i: nav) {  
%><li <%= i.hasChildren() %>><a href="<%= i.getPath() %>.html"><%= i.getTitle() %></a>      <%
          break;
 }

But if I add in a switch statement within the for loop it displays 1 of each navigation element like it should. 但是,如果我在for循环中添加一个switch语句,它会像应该显示的那样显示每个导航元素中的1个。

for (Navigation.Element i: nav) {  
     switch (i.getType()) {
     case ITEM_BEGIN:
          %><li <%= i.hasChildren() %>><a href="<%= i.getPath() %>.html"><%=     i.getTitle() %></a><%
          break;
  }
 }

This is driving me crazy, any help is greatly appreciated! 这让我发疯,非常感谢您的帮助! Thanks! 谢谢!

you can try this code snippet : 您可以尝试以下代码片段:

    <%
    Navigation navRoot = new Navigation(currentPage,2,new PageFilter(request),4);
    for (Navigation.Element e: navRoot) {
        switch (e.getType()) {
            case NODE_OPEN:
            %><ul><%
                break;
            case ITEM_BEGIN:
                %><li ><a href="<%= e.getPath() %>.html"><%=     e.getTitle() %></a> <%
                break;
            case ITEM_END:
            %></li><%
                break;
            case NODE_CLOSE:
            %></ul><%
                break;
        }
    }

    %>

sample component in out of box instance is at location : /apps/geometrixx/components/topnav 开箱即用实例中的示例组件位于以下位置:/ apps / geometrixx / components / topnav

I do not know the exact reason for 2 of each navigation element. 我不知道每个导航元素中2个的确切原因。 But CQ5 documentation on " Navigation " states that 但是有关“ Navigation ”的CQ5文档指出:

"A navigation element reflects a page and can have different Navigation.Element.Types . Note that the same page might be returned 4 times for the different element types. this offers maximal flexibility when drawing the navigation." “导航元素反映了一个页面,并且可以具有不同的Navigation.Element.Types 。请注意,同一页面可能针对不同的元素类型返回4次。这在绘制导航时提供了最大的灵活性。”

Possibly the same element is being returned 2 times for element types. 对于元素类型,可能相同的元素被返回2次。 If you put the switch-case block you are selecting a particular element type and hence shows only once. 如果放置开关盒块,则选择的是特定的元素类型,因此仅显示一次。

Perhaps the key to your answer lies in Navigation.Element.Types . 可能答案的关键在于Navigation.Element.Types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM