使用JavaServer Pages(jsp)创建下拉式导航栏

huangapple 未分类评论38阅读模式
英文:

Creating a dropdown navbar using JavaServer Pages (jsp)

问题

<% for (int i = 0; i < navbar.length; i++) {
    if (navbar[i].length == 1) { %>
        <li><a href="<%= navbar[i][0][1] %>">Home</a></li>
    <% }
    if (navbar[i].length > 1) { %>
        <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" href="#"><%= navbar[i][0][0] %><span class="caret"></span></a>
            <ul class="dropdown-menu">
                <% for (int j = 1; j < navbar[i].length; j++) { %>
                    <li><a href="<%= navbar[i][j][1] %>"><%= navbar[i][j][0] %></a></li>
                <% } %>
            </ul>
        </li>
    <% }
} %>

请注意,上述代码是你提供的 JSP 代码的修正版本,已经根据问题描述进行了修复。如果你将上述代码粘贴到你的 JSP 文件中,应该可以正确地显示下拉菜单中的所有项。

英文:

I am trying to create a dropdown menu navbar using jsp, from the following 3D array.

String[][][] navabr = new String[][][]
      {
             
              {{&quot;pageA&quot;}, {&quot;page1&quot;, &quot;subpageA_1_link_url&quot;}, {&quot;page2&quot;, &quot;subpageA_2_link_url&quot;}},
              {{&quot;pageB&quot;},{&quot;page2&quot;, &quot;subpageB_1_link_url&quot;}, {&quot;page2&quot;, &quot;subpageB_2_link_url&quot;}, {&quot;page3&quot;, &quot;supageB_3_link_url&quot;}}
      };

This is the code snippet that i have implemented to try and produce it

&lt;% for (int i = 0; i &lt; navbar.length; i++){
if (navbar[i].length == 1){
    %&gt;&lt;li &gt;&lt;a href=&quot;#&lt;%= navbar[i][0][1]%&gt;&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  &lt;% } %&gt;
  &lt;%if (navbar[i].length&gt;1){
      %&gt;&lt;li class=&quot;dropdown&quot;&gt;&lt;a class=&quot;dropdown-toggle&quot; data-toggle=&quot;dropdown&quot; href=&quot;#&quot;&gt;&lt;%= navbar[i][0][0]%&gt; &lt;span class=&quot;caret&quot;&gt;&lt;/span&gt;&lt;/a&gt;

      &lt;%}
      for (int j = 1  ; j &lt; navbar[i].length; j++){
          %&gt;
              &lt;ul class=&quot;dropdown-menu&quot;&gt;
                &lt;li&gt;&lt;a href=&quot;#&lt;%= navbar[i][j][1] %&gt;&quot;&gt;&lt;%= navbar[i][j][0] %&gt;&lt;/a&gt;&lt;/li&gt;

              &lt;/ul&gt;
      &lt;%}
}%&gt;

&lt;/li&gt;

&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;

The problem that im having is that the drop down items are not getting looped, rather, only the last item is shown. For excample, for dropdown of pageA, only page 2 is shown and for the drop down of pageB only page 3 is shown. How do i fix it to have it show all dropdowns

huangapple
  • 本文由 发表于 2020年7月25日 20:06:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/63088120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定