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