A Custom JSP Tag Library for Dynamic Menus
by Prabu Arumugam04/09/2003
Menus are critical components of software applications. Implementing a static menu is relatively easy--just categorize and customize the application's functionality once. Implementing a dynamic menu, unique for each user depending on his or her profile and preferences, is both challenging and cumbersome.
While the Java programming language has built-in support to create basic menu structures, JSP lacks support. Web applications must use either Java applets or JavaScript to implement menu structures. Many web application developers prefer JavaScript to applets for simplicity and ease of deployment. This article describes a custom tag library that simplifies the process of generating JavaScript dynamically. The design and implementation of the tag library are covered in detail.
Design Approach
Our design goal is an object model that represents hierarchical data and contains methods to generate JavaScript.
Each menu in the hierarchical menu system can contain one or more simple menu items or sub menus, which in turn may contain simple menu items and submenus. The simple menu provides access to the application-specific functionality. Regardless of the menu type, the menu object should be capable of generating appropriate JavaScript.
Based on a composite design pattern, the simple menu represents the menu at
the leaf level, while the composite menu represents groups of submenus and
simple menus in the menu structure. In other words, the Composite can contain a
list of SimpleMenu objects and other CompositeMenu
objects. The Menu abstract class represents a menu in the
hierarchy and defines a render method for subclasses to provide
their own implementations.
Figure 1 illustrates the composite design pattern:

Figure 1. UML for our menu classes
The SimpleMenu renders menu items with URLs that point to
application functionality. The CompositeMenu overrides the
render method by looping through its list of submenus, calling
render on each. Remember, lists can contain both menu items and
sub menus, so menus can be multiple levels deep.
Listing 1 shows the render method of
CompositeMenu and SimpleMenu. Both methods just
produce JavaScript, which calls existing functions defined in dynamicmenu.js. Functions in this file are responsible for creating menus in the browser. The design and the
implementation of these functions are beyond the scope of this article.
The render Method of SimpleMenu
StringBuffer sb = new StringBuffer();
sb.append("addmenuitem(");
sb.append("\"" + getLevelCoord() + "\",");
sb.append("\"" + getMenuName() + "\",");
sb.append("\"" + getUrl() + "\",");
sb.append("\"black\",\"FAEBD7\",\"white\", \"3366CC\",\"white\",
\"3366CC\",\"font-family:Tahoma, Verdana, Arial;
font-size:12px;font-weight:normal,text-decoration:none;padding: 4px\");");
sb.append("\n");
return sb.toString();
The render Method of CompositeMenu
StringBuffer sb = new StringBuffer();
sb.append("addmenuitem(");
sb.append("\"" + getLevelCoord() + "\",");
sb.append("\"" + getMenuName() + "\",");
if (null == getUrl())
sb.append("null" + ",");
else
sb.append("\"" + getUrl() + "\",");
sb.append("\"black\",\"FAEBD7\",\"white\",\"3366CC\",\"white\",\"3366CC\",
\"font-family:Tahoma, Verdana, Arial; font-size:12px;
font-weight:normal,text-decoration:none;padding: 4px\");");
sb.append("\n");
Iterator it = list.iterator();
int i=1;
while(it.hasNext())
{
Menu menu = (Menu)it.next();
menu.setLevelCoord(getLevelCoord() + "," + i);
sb.append(menu.render());
i++;
}
return sb.toString();
Menu Data Source
The menu builder is responsible for building menus from the data source. Menu data is hierarchical by nature. It can come from any source that supports hierarchical data representation. We have implemented builders for two commonly used data sources: XML and a database. However, it is easy to develop other builders using this approach.
XMLMenuBuilder
Since menu structures and XML structures are both hierarchical, XML can
easily represent menu data. XMLMenuBuilder accepts the name of an
XML file that contains menu definitions and parses the XML document to build
the dynamic menu. The <menu> element in the XML document can
represent menu items or submenus, which in turn can contain menu items or
submenus.
JDBCMenuBuilder
Despite the fact that database tables represent relational data, tables can
still be used to represent hierarchical data. The HIERARCHYMENU
table represents hierarchical menu data. JDBCMenuBuilder accepts
database source information in order to select menu data from the
HIERARCHYMENU table.
Menu Tag Library
The menu tag library manages the complex process of creating menus in
JavaScript. The menu tag itself is an abstract class that extends the
TagSupport class and overrides the doStartTag and
doEndTag methods. The getMenu method, which is a
template method and should be overridden in the subclasses, provides JavaScript
to add menu items in the menu structure created in the doStartTag
method. Subclasses of the menu tag override the getMenu method,
which uses menu builders to render menu data from the data source.
The Dynamic Menu Tag Library contains two tags: JDBCMenuTag and
XMLMenuTag. JDBCMenuTag uses
JDBCBuilder to build the menu structure from a database source,
while XMLMenuTag uses XMLBuilder to build the menu
structure from an XML data source. The documentation provides complete
instructions for using the tag library.
The current version of this tag does have a few limitations, however. It does not yet comprehend the association between the user and the menu, which is different for each application. Also, the library does not provide support for specifying the color or font for menu items.
|
Download Source Code |
Menu Tag Sample Application
The menu-example.war file contains JSP pages that use this tag library. Follow these instructions to deploy the application on Tomcat:
- Copy menu-example.war to tomcat installation folder\webapps.
- Restart Tomcat to deploy the application.
- Adjust xmlmenu.jsp and jdbcmenu.jsp to fit your environment.
- Create a new database table structure and import seed data by running the menu.sql file.
- To access the JSP that builds menus from XML, visit localhost:8080/menu-example/xmlmenu.jsp.
- To access the JSP that builds menus from JDBC, visit localhost:8080/menu-example/jdbcmenu.jsp.
Conclusion
The Dynamic Menu Tag Library manages the complex process of creating menus in JavaScript, and allows web applications to build dynamic, hierarchical menus from a database or an XML source.
Resources
- For more information on menu tags, view the menutag.html documentation
- Design Patterns, Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley Publishing Co., 1995; ISBN: 0201633612)
- JSP Tag Libraries
Prabu Arumugam is a software architect and senior Java developer at Forest Express, LLC.
Return to ONJava.com.
-
not able to make link on TOP and single menu
2010-03-25 05:04:35 menuDeveloper [View]
-
Displaying the menu inside the Table Tag
2006-12-10 20:15:39 DynamicMenus [View]
-
help on class attribute
2006-11-23 05:05:05 hsp2816 [View]
-
dynamically change space between top menu items based on topmenu items length
2006-03-20 11:12:00 javaenthu [View]
-
Dynamic Sub menu position
2005-12-20 08:20:13 priya123 [View]
-
Will it work in Jdeveloper 9.0.3
2005-09-20 06:06:30 khalidFahd [View]
-
How to make the top level different from the rest of the menu.
2005-08-08 12:15:04 wshami [View]
-
dynamic tree structure with jsp and oracle
2005-07-05 00:06:19 Selvi [View]
-
Menu options for sample
2005-05-05 13:37:10 kenshinmax [View]
-
doubt regarding how to deploy
2005-04-22 17:26:42 anjaliiiii [View]
-
Menu Errors on IE 6 ???
2005-03-05 17:18:27 kid1981 [View]
-
inactive menu item
2005-02-22 04:13:00 ataer [View]
-
menu
2005-02-05 01:14:35 shivajava [View]
-
jdbcmenutag
2004-12-20 21:23:01 zeesunil [View]
-
URGENT... regd. JDBCMENUTAG
2004-12-20 06:06:53 zeesunil [View]
-
DOUBT REGD. JDBCMenuTag
2004-12-20 02:12:41 zeesunil [View]
-
doubt regd. jdbcmenutag
2004-12-20 00:41:28 zeesunil [View]
-
the same problem...
2004-12-14 00:05:31 gino1203 [View]
- Trackback from http://blog.csdn.net/tl_ang/archive/2004/10/29/158525.aspx
åå»ºå¨æèåçJSPèªå®ä¹æ ç¾ï¼
2004-10-29 00:13:49 [View]
-
Please Help - Thank you.
2004-07-01 02:44:19 Dazzler [View]
-
jdbcmenu.jsp error
2004-06-28 13:49:58 sumanish [View]
-
JSP Tag Libraries
2004-06-10 06:45:42 fj_rodriguez [View]
-
JavaScript errors with Netscape 4.7 & menu-example.war
2004-03-08 02:34:09 cwele [View]
-
problem with menu inside <TD> in IE
2004-02-24 03:52:25 asanjuin [View]
-
help me please?
2004-02-06 14:32:25 camilo79 [View]
-
how to load menu-example on tomcat
2003-11-21 06:52:08 rexam [View]
-
How to configure Dynamic menu for User and Access Right
2003-11-13 01:26:53 ashu_iit [View]
-
has some problem in using
2003-09-12 04:24:00 anonymous2 [View]
-
how do i split the menu bar?
2003-08-26 01:39:40 anonymous2 [View]
-
How to add more parameters
2003-06-16 11:43:27 amitdas91 [View]
-
How do I change the Position of the Menu?
2003-05-21 10:36:55 venu_dvmr [View]
-
the menu always tries to position itself at the top-left corner of screen
2003-05-20 11:05:23 anonymous2 [View]
-
for a comparison
2003-05-05 03:49:05 anonymous2 [View]
-
color needs # to work on mozilla+netscape
2003-04-23 07:03:14 anonymous2 [View]
-
JavaScript part of this product is developed by SoftComplex Inc.
2003-04-19 11:58:14 anonymous2 [View]
-
Please provide description on how to set-up Menu properties in XML file?
2003-04-19 07:49:09 anonymous2 [View]
-
JavaScrip Menu Documentation
2003-04-19 07:25:38 anonymous2 [View]
-
JavaScrip Menu Documentation
2003-04-19 07:19:38 anonymous2 [View]
-
all the files there?
2003-04-10 09:04:39 anonymous2 [View]