Java-JSP Interface for HierMenus (4/4) | WebReference

Java-JSP Interface for HierMenus (4/4)

To page 1To page 2To page 3current page

A Java-JSP Menu Builder for HierMenus

Replace the Hierarchical Menu HM_Arrays.js

The HierMenus JavaScript file HM_Loader.js has default parameters that set up the look and feel of the menu. This file internally loads HM_Arrays.js and the Netscape and IE specific JavaScript files needed to drive the menu. To replace the HM_Arrays.js, you have to follow these steps:

  • Copy and paste the JSP code below into a new file and save it as menu.jsp.

  • Save HM_Loader.js, HM_ScriptNS4.js, HM_ScriptIE4.js and HM_ScriptDOM.js (all from the HierMenus distribution) in the directory structure under the \js folder.

  • Comment the following line in HM_Loader.js (line wrapped here for better formatting on this page):

    document.write("<SCR" + "IPT LANGUAGE='JavaScript1.2' 
                    TYPE='text/javascript'><\/SCR" + "IPT>");

    This line is removed as we won't be using HM_Arrays.js anymore.

Your final directory structure should look like this

{webapp_directory}\menu-app\js\ HM_ScriptNS4.js
{webapp_directory}\menu-app\js\ HM_ScriptIE4.js 
{webapp_directory}\menu-app\js\ HM_ScriptDOM.js
{webapp_directory}\menu-app\WEB-INF\classes\ HMContentTreeRoot.class
{webapp_directory}\menu-app\WEB-INF\classes\ HMContentTreeItem.class
{webapp_directory}\menu-app\WEB-INF\classes\ MenuContentBuilder.class

If you are using tomcat, webapp_directory is tomcat's webapps directory, and menu-app is the Web application directory containing JSP, HTML, JavaScript and class files.

Below is the listing for a menu JSP that adds the dynamic generation of the menu contents and substitutes the static HM_Arrays.js.

<%@ page language="java"  buffer="32kb"%>
       //MenuContentBuilder is the java class that builds the menu tree content
      HMContentTree menuContentTree = MenuContentBuilder.getTree("Paulo");
      String menuContent = menuContentTree. toHMContent();
 <script language="javascript">
<% out.write(menuContent); %>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
if(window.event + "" == "undefined") event = null;
function HM_f_PopUp(){return false;};
function HM_f_PopDown(){return false};
popUp = HM_f_PopUp;
popDown = HM_f_PopDown;
HM_a_TreesToBuild = [1];
<SCRIPT LANGUAGE="JavaScript1.2" SRC="js/HM_Loader.js" TYPE='text/javascript'></SCRIPT>
  <a href="#" 
    onClick="return false;" 
    onMouseOver = "popUp('HM_Menu1',event)" 
    onMouseOut = "popDown('HM_Menu1')"><b>Please bring your mouse over here</b></a>&gt;
  <IMG src="tri.gif" width=7>

In the listed JSP code, the generation of the menu contents in the proper format happens in two places:

  1. HMContentTree menuContentTree = MenuContentBuilder.getTree("Paulo");
    MenuContentBuilder constructs the HMContentTree object based on the user parameter. The MenuContentBuilder class can access a database, EJB or some other data source for retrieving the user specific menu content.

  2. String menuContent = menuContentTree.toHMContent();
    The menu contents are generated in the menuContent string. HMContentTree is provided with the toHMContent() method, which navigates the tree structure and generates the proper representation as in the HM_Arrays.js in the HierMenus distribution.

  3. out.write(menuContent);
    The menuContent string is written to the output stream.


This article provides you with listings for HMContentTree, HMContentTreeRoot, HMContentTreeItem and a sample MenuContentBuilder class. It also presents menu.jsp, which brings the content dynamism obtained through these Java classes into the existing HierMenus JavaScript files.

About the authors

Paulo Caroli,, MSc in Software Engineer, is a Senior Java Developer with more than 8 years of experience in Application Development. He is an expert in Object Oriented techniques and has been using J2EE in Web Applications for the past 4 years. He currently works for OmniPros (

Srimant Misra is a Senior Consultant with 6 years of experience in Information Technology. He's an expert in OOAD and performance monitoring of J2EE applications. He currently works for eTouch Systems (

To page 1To page 2To page 3current page

Created: November 13, 2002
Revised: November 13, 2002