This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes menu creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "menu creation " Watch "menu creation " New topic
Author

menu creation

sharathchandra sabbani
Greenhorn

Joined: Oct 15, 2007
Posts: 13
How to create a menu using struts???
andres matus
Greenhorn

Joined: Dec 21, 2007
Posts: 15
First off all you have to know what kind of menu do you want. If it�s a DB based menu, or a static menu, and depending of that select the best way to implement it. In most cases you can choose javascript and get really cool menus, but my suggest is to use struts-menu, you can do almoust everything with that.

take a look at http://struts-menu.sourceforge.net/


Be free...do what ever you whant
sharathchandra sabbani
Greenhorn

Joined: Oct 15, 2007
Posts: 13
i dont know what is db menu and static menu ... but i want to create a menu if administrtor logs into the system one extramenu should be displayed if an employee logs into the system it should only display their related menu... how to do this please help
andres matus
Greenhorn

Joined: Dec 21, 2007
Posts: 15
a DB based menu is a Data Base based menu, wich means that all the topics in the menu are obtained from a data base, that way you make your menu be dynamic. On the other hand if you dont need a dynamic menu, you can build it with javascript, or with html lists and give'em style with css.

First off all you need to tell what kind of menu do you need.

If one requirement is to have diferent menus for diferent users, you should have each menu related with the users.

I did this a few time a go...so be more especific to be able to help you.
sharathchandra sabbani
Greenhorn

Joined: Oct 15, 2007
Posts: 13
i need a database menu...
andres matus
Greenhorn

Joined: Dec 21, 2007
Posts: 15
ok then....finally we get there

whe need a lot of work here so i hope you can implement it....

We are going to use struts-menu...

Step 1.

Include the struts-menu library in your proyect class path (i'm using version 2.4.2).

Step 2.
Setting the plug-in in the struts-config.xml...

< plug-in className="net.sf.navigator.menu.MenuPlugIn" >
<set-property property="menuConfig" value="/WEB-INF/menu-config.xml"/>
</plug-in >

the menu-config.xml file is the configuration for the displayer...you gonna get it later....here is an example...

<?xml version="1.0" encoding="UTF-8" ?>
<MenuConfig>
<Displayers>
<Displayer name="ListMenu" type="yourpackage.ListMenuDisplayer" />
</Displayers>
</MenuConfig>

the ListMenuDisplayer is the class that build the menu....


public class ListMenuDisplayer extends MessageResourcesMenuDisplayer {
//~ Instance fields ========================================================

//~ Methods ================================================================

public void init(PageContext pageContext, MenuDisplayerMapping mapping) {
super.init(pageContext, mapping);

}

public void display(MenuComponent menu) throws JspException, IOException {
if (isAllowed(menu)) {
if(menu.getParent() == null)
{
out.println("<ul width=\"150px\">");
displayComponents(menu, 0);
out.println("</li></ul>");
}
else
{
displayComponents(menu, 0);
}
}
}

protected void displayComponents(MenuComponent menu, int level)
throws JspException, IOException {


MenuComponent[] components = menu.getMenuComponents();

if(menu.getParent() == null )
{
out.print("<li >");
out.println("<h2 style=\"font-size: 14px;\">");
out.println(menu.getTitle());
out.println("</h2><ul>");
for (int i = 0; i < components.length ; i++ )
{
display(components[i]);
}
out.println("</ul>");
}
else
{
if(components.length > 0)
{
out.println("<li><a class=\"x\" style='font-size:13px;'>");
out.println(menu.getTitle());
out.println("</a><ul>");
for (int i = 0; i < components.length ; i++ )
{
display(components[i]);
}
out.println("</ul></li>");
}
else
{
if(menu.getLocation() != null)
{
if(!(menu.getLocation()).startsWith("window")){
out.println("<li><a href=\" " + menu.getLocation() +" \" style='font-size:13px;'>");
out.println(menu.getTitle());
out.println("</a></li>");
}
else
{
out.println("<li><a onKlick=\" " + menu.getLocation() +" \" style='font-size:13px;'>");
out.println(menu.getTitle());
out.println("</a></li>");
}
}
else
{
out.println("<li><a disabled style='color: gray; cursor: default;font-size: 12px;' >");
out.println(menu.getTitle());
out.println("</a></li>");
}
}

}
}

/**
* This will output the ending HTML code to close tags from the beginning
* @param context the current pageContext
*/
public void end(PageContext context) {
try {
out.print("");
} catch (Exception e) {
log.error(e.getMessage());
}
}

public String getExtra(MenuComponent menu) {
StringBuffer extra = new StringBuffer();
if (menu.getTarget() != null) {
extra.append(" target=\"").append(menu.getTarget()).append("\"");
}
if (menu.getOnclick() != null) {
extra.append(" onklick=\"").append(menu.getOnclick()).append("\"");
}
if (menu.getOnmouseover() != null) {
extra.append(" onmousover=\"").append(menu.getOnmouseover()).append("\"");
}
if (menu.getOnmouseout() != null) {
extra.append(" onmousout=\"").append(menu.getOnmouseout()).append("\"");
}
if (menu.getWidth() != null) {
extra.append(" style=\"width: " + menu.getWidth() + "px\"");
}
return (extra.length() > 0) ? extra.toString() : "";
}

this class is renders the menu when you declare it...i will do this later...


Step 3.

Now you need to build the menu based on your information from the data base, if you have functions then the only hing you need is to make a list of that and iterate through them to build it. Also you can define the link of the menu, and set it on the location of the menu component...

You can build the menu from any class you whant, i did it at the login class, so when the user logs in the menu is built and at the redirect is available.....put this lines where you whant to build it....

//declaring the repository
MenuRepository repository = new MenuRepository();
//getting the displayers info from the xml file
MenuRepository defaultRepository = (MenuRepository)
getServlet().getServletContext().getAttribute(MenuRepository.MENU_REPOSITORY_KEY);

repository.setDisplayers(defaultRepository.getDisplayers());

//class MenuBuilder that i create, see next class...
MenuBuilder menuBuilder = new MenuBuilder();

repository = menuBuilder.buildMenu(user,repository,allFunctions);

//after you build the menu put it on the session so you dont have to rebuildit again...improving performance...

request.getSession().setAttribute("repository",repository);


/*** Getting inside the menu building steps ***/
MenuBuilder class

public class MenuBuilder
{

public MenuRepository buildMenu(Users us,MenuRepository rep,List functions)
{

List allFunctions = functions;
Hashtable userFunctions = new Hashtable();
Hashtable titulosNoRepetidos = new Hashtable();

//Class that set the cmenu component...see next class
MenuDisplay md = new MenuDisplay();

Iterator it0 = allFunctions.iterator();
while(it0.hasNext()){
//Functions is an object that haves the title and link of the menu.
Functions function = (Functions)it0.next();
rep.addMenu(md.menuDisplay(function, rep));
}
return rep;
}
}


/*******************************************/

public class MenuDisplay
{
public MenuDisplay()
{
}

public MenuComponent menuDisplay(Functions function,MenuRepository repository)
{
MenuComponent mc = new MenuComponent();
String name = function.getItemName();
mc.setName(name);
String parent = function.getParentName();

if (parent != null) {
MenuComponent parentMenu = repository.getMenu(parent);
if (parentMenu == null) {
// create a temporary parentMenu
parentMenu = new MenuComponent();
parentMenu.setName(parent);
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu);
}
String title = function.getItemTitle();
mc.setTitle(title);
String location = null;

if(function.getItemLocation() != null && !(function.getItemLocation()).startsWith("window"))
{
location = function.getItemLocation();
}
else{
location = null;
location = function.getItemLocation(); }
mc.setLocation(location);
return mc;
}
}


Ok....at this point you should be able to have the menu...be carefull to make the Data base structure to the menu, you at least need a name,location,and parent, to have a tree relation of the menu.

Now we need to display the menu on the jsp pages....

Step 4.

i made a .jspf page (that is a fragment of a jsp...) and include it in all other jsp pages where i need the menu to be displayed.


menu.jspf

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="http://struts-menu.sf.net/tag" prefix="menu" %>
<%@ taglib uri="http://struts-menu.sf.net/tag-el" prefix="menu-el" %>

<%
net.sf.navigator.menu.MenuRepository repository = (net.sf.navigator.menu.MenuRepository)(request.getSession().getAttribute("repository"));
%>
<table width="1270px" >
<tr>
<td>
<div id="menu">
<menu-el:useMenuDisplayer name="ListMenu" repository="repository" >
<logic:iterate id="headers" name="repository" property="topMenus" type="net.sf.navigator.menu.MenuComponent">
<c:if test="${headers.name != null}">
<menu-el isplayMenu name="${headers.name}" />
</c:if>
</logic:iterate>
</menu-el:useMenuDisplayer>
</div>
</td>
</tr>
</table>

this jspf page display the menu iterating over the repository you set up on the session on step 2....
request.getSession().setAttribute("repository",repository);


so now the only thing you need to do is include this jspf page into others jsp pages to see the menu....
<%@ include file="menu.jspf"%>


and thats it.....

hope i could help you with this....

gonna what for your reply....
Dilip H Pashupathi
Greenhorn

Joined: Jul 24, 2013
Posts: 22
Hi,

Does this support special chars like '_' symbols? I have a same scenario where I need to generate a tree menu out the data contained in the db
like

something like this.

Thanks and regards
Dilip
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8705
    
    6

Since this post was last touched in 2007, I don't think these guys are coming back.

Dilip H Pashupathi wrote:
Does this support special chars like '_' symbols?


Did you try it? I'd think you could get an example working in a couple minutes. Looks like struts-menu was last updated in 2007 as well.
Personally, I'd gravitate to the menu component in the Struts jQuery plugin


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: menu creation
 
Similar Threads
Can i use Menu & Menu options in applet?
Trouble with displaying fonts
collapsable list
Menu implementation in Struts2
calling a servlet by clicking a menu?