• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL - Dynamic dropdown/dropright menu creation in JSP, extracting data from a list  RSS feed

 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MVC pattern: servlets + JSP pages.
I have a class `Category`with (basic) look:


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).

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.



Now, in JSP page, i have a navigator menu, you can see it here: http://jsfiddle.net/vvozar/tMz9A/1/
(`see menu look in jsfiddle link`)
Dropdown menus are simple unordered lists in HTML, and its submenus are nested unordered lists under certain menu items. Example (static):


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). I dont know how to approach, because i dont know how deep will submenus go.

How should JSTL loop for creating categories menu look?
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First question to ask is: Is a List the right collection to use?

From my understanding, you are having categories and their sub-categories all in a list. Assuming you have such list, how do you tell without checking the parentCategory attribute whether it's a top category or sub-category?


I'm thinking using a map with the top category as key and a list of (sub) categories as the value. eg Map<Category, List<Category>>

Then in JSTL you can use the <c:foreach> tag to loop through your collection.
 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@K. TSang - well, i had that option on my mind too. And yes, categories and subcategories are all together in one list. Only way to know if the category is top level, is to check its parentCategory field.
As you can see i am using categoryDao.findAll(); and this method returns all categories from database:

public List<Category> findAll() {
Query q = em.createQuery("SELECT x FROM Category x");
List<Category> result = q.getResultList();
return result;
}

 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Hibernate?

Anyway I'm sure you can find a way to pass a map to the JSP if you really wanted to.
 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JPA. I will try tomorow to find solution using Map instead of list (don't have time today), and then will post here what have i done.
 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This what i have now:
servlet part:


Now, in application scope i have a map which elements are pairs(category, set-of-its-subcategories).
So, (i guess) i need proper JSTL foreach loop that will generate unordered html lists for submenus.
How to implement this foreach loop?
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the JSTL the items attribute is your map which is Map.Entry. Then you can get the key and values


 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, this is so confusing. I understand the code you posted, but still don't know how to generate for loop that will print all the submenus, because i don't know 'how deep' will submenus go.

I would like dynamicly, in a loop, reading data from map, to create something similar to this:



Hope you understand whats the issue?
 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to limit subcategories level. I have limited it to level 3. And i have implemented this using list.
So, this is the code in servlet:


And these are the loops in JSP page, where am i extracting data from list and generating html markup:
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your problem is only "don't know how many levels of sub-menus" then doesn't the list tells you eg list.size()?

But then not all items in the list are sub-menu, isn't it? The question to ask is why is there so many sub-menus?



This also applies to the map. The value is a list of categories.
 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:If your problem is only "don't know how many levels of sub-menus" then doesn't the list tells you eg list.size()?

But then not all items in the list are sub-menu, isn't it? The question to ask is why is there so many sub-menus?

This also applies to the map. The value is a list of categories.


Well, list.size() only returns all the categories, but doesn't answer the subcategory depth.
Application i am trying to implement is simple Blog application, where Category represents a category that post belongs to.
Then, administrator can manage categories and subcategories. Can add subcategories. So subcategories depth is dynamic.

Anyway, its a simple app and limiting subcategories level to 3 is acceptable.
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. It looks to me you are using 1 class fits all type of design making loading the list or map or whatever collection recursive X times ... just don't know what X is.

This can be improved but your design would need to be changed quite a bit I think.

But that's another day and topic
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!