Note that there are some explanatory texts on larger screens.

plurals
  1. POJSTL - Dynamic dropdown/dropright menu creation in JSP, extracting data from a list (java.util.List)
    primarykey
    data
    text
    <p>MVC pattern: servlets + JSP pages. I have a class <code>Category</code>with (basic) look:</p> <pre><code>public class Category{ private Integer id; private String name; private Category parentCategory; ... //getters and setters } </code></pre> <p>This class should represent category-subcategory concept: one category can have multiple subcategories, category does not need to have a parent (top level category, parentCategory field is null), if it is a subcategory then its parentCategory field represents its parent).</p> <p>In servlet, i extract all categories from database to a java.util.List object. Then i attach this list to application scope(servlet context) and forward it to JSP page.</p> <pre><code>List&lt;Category&gt; categories = categoryDao.findAll(); ServletContext servletContext = getServletContext(); servletContext.setAttribute("categories", categories); </code></pre> <p>Now, in JSP page, i have a navigator menu, you can see it here: <a href="http://jsfiddle.net/vvozar/tMz9A/1/" rel="nofollow">http://jsfiddle.net/vvozar/tMz9A/1/</a> (<code>see menu look in jsfiddle link</code>) Dropdown menus are simple unordered lists in HTML, and its submenus are nested unordered lists under certain menu items. Example (static):</p> <pre><code>&lt;nav&gt; &lt;!-- Navigation --&gt; &lt;ul class="dropdownMenu"&gt; &lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Categories&lt;/a&gt; &lt;ul class="droprightMenu"&gt; &lt;li&gt;&lt;a href="#"&gt;All&lt;/a&gt; &lt;ul class="droprightMenu"&gt; &lt;li&gt;&lt;a href="#"&gt;Category 1&lt;/a&gt; &lt;ul class="droprightMenu"&gt; &lt;li&gt;&lt;a href="#"&gt;Category 1.1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Category 1.2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Category 1.3&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Category 1.4&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Category 2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Category 3&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Category 4&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Manage&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Profile&lt;/a&gt; &lt;ul class="droprightMenu"&gt; &lt;li&gt;&lt;a href="#"&gt;Login&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Register&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Edit Profile&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;My Posts&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Logout&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Help&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/nav&gt; </code></pre> <p>I would like to create these menus and submenus (dynamically) by reading categories/subcategories from the list, following category hierarchy.(categories with same parent belong to same dropdown menu).</p> <p>How should JSTL loop for creating categories menu look?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload