my dog learned polymorphism*
The moose likes JSP and the fly likes Generate Menu from Database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Generate Menu from Database." Watch "Generate Menu from Database." New topic
Author

Generate Menu from Database.

Paddu Hegde
Greenhorn

Joined: Aug 31, 2008
Posts: 5
I want to generate dynamic menu according to the user roles. This i want to achieve with JSP.

Currently i have a javascript for menu design and effects. I want the following steps done which is pending :

1.Create a Menu entries into a database
2.Render menu items from database
3.Have one field as "role" employee and customer. Now based on customer or employee the menus should be displayed.

Kindly suggest me how to go about this. It would be great if a sample code shared.

Thanks a lot for your help.

Thanks and Rgds,
Kino
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

"Paddu xdd", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

What code have you written so far? What has you stumped?
Paddu Hegde
Greenhorn

Joined: Aug 31, 2008
Posts: 5
Originally posted by Bear Bibeault:
What code have you written so far? What has you stumped?


I have completed javascript coding for menu design only. In JSP how to go about is a big question mark for me now!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

If the menu is working, what do you need to do in the JSP? Are there dynamic parts of the menu that need to be filled in before sending the HTML page to the browser?

Remember that JSP is just a templating mechanism to dynamically generate the HTML page sent to the client. So while JSP can help you create the HTML and JavaScript that implements the menu, the structure and behavior of the menu is still controlled by HTML and JavaScript.

So once you have the HTML and JavaScript for the menu settled (which it sounds like you already have), it simply a matter for using the JSP mechanisms to fill in the dynamic parts that can;t be ahrd-coded in advance.
Paddu Hegde
Greenhorn

Joined: Aug 31, 2008
Posts: 5
Originally posted by Bear Bibeault:
If the menu is working, what do you need to do in the JSP? Are there dynamic parts of the menu that need to be filled in before sending the HTML page to the browser?

Remember that JSP is just a templating mechanism to dynamically generate the HTML page sent to the client. So while JSP can help you create the HTML and JavaScript that implements the menu, the structure and behavior of the menu is still controlled by HTML and JavaScript.

So once you have the HTML and JavaScript for the menu settled (which it sounds like you already have), it simply a matter for using the JSP mechanisms to fill in the dynamic parts that can;t be ahrd-coded in advance.



Hi Again,

JSP is pretty new to me. Since my project needs that i use JSP, i have come to this forum ;-)

yes you are right javascript is pefectly working for me. My question here now is how to impplement in such a way that the menu items from Database is used by javascript from JSP connection?

I would appreciate a code sample.

Thanks and Rgds,
Pradeep
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

You would use JSP mechanisms to take the information from the database (but under no circumstances should you do DB access in the JSP itself -- delegate that to a model class) and include it in the HTML markup.

If you are new to JSP, I'd suggest working through a JSP tutorial (make sure that it's up-to-date, concentrating on JSTL and EL, and not putting Java code in the JSP) to familiarize yourself with JSP technology.

Also, read these articles from the JavaRanch Journal:

The Secret Life of JavaServer Pages
The Front Man
[ September 01, 2008: Message edited by: Bear Bibeault ]
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Role Based Menu?

Suggestions,
Suppose if you are using any struts framework or if you have any welcome page or particular url pattern,

1. write a filter named RoleMenuFilter,which should get invoked for all the url patterns
2. it should get the user details from session or request and
scan db for its role and menu for this role.

Rolemaster table has roles
Menu master table would have menus and linked roles

MENU MASTER
ID, MENUNAME, MENUDISPLAYNAME, MENU URL, PARENTMENU
parentmenu field is optional, if you are not going for nested menus

ROLES MASTER
ROLEID, ROLENAME

ROLES_MENU [joining table]
ROLE_ID, MENU_ID
foreign keys to above master tables

Now scan these tables to get list of menus for this user and keep it in request attribute.


now while displaying the naviagation portion

<c:forEach var="menuItem" items=${menuList}>
you can dislay menuItem.DisplayName, menuItem.Url
</c:forEach>

Hope it helps


SCJP 1.4 SCWCD 1.4 SCDJWS 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Well, the OP didn't say he was using Struts, and did not say that menu was user-specific. But if so, I would not use the filter approach. That incurs unnecessary overhead on each request. And what are the chances that the menu information needs to be known for each and every request? Rather, I'd set up any role-specific information in the session once at authentication time.
Paddu Hegde
Greenhorn

Joined: Aug 31, 2008
Posts: 5
Originally posted by Bear Bibeault:
Well, the OP didn't say he was using Struts, and did not say that menu was user-specific. But if so, I would not use the filter approach. That incurs unnecessary overhead on each request. And what are the chances that the menu information needs to be known for each and every request? Rather, I'd set up any role-specific information in the session once at authentication time.


Hi Bear, Hi All,

Thanks for your tips..

Now i am able to retrieve all the data from database through java.

Now my next step is to pass this data to a html page. Any idea?

Thanks and Rgds,
Pradeep
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generate Menu from Database.