Struts/JSP Menu Tag Library
I have worked on many web projects where the programmers would create a menu system with the combination of JavaScript, JSP custom taglibs, and a database to store the menu tree. This to me always seemed a little bit complicated because:
- There were too many moving parts
- Difficult to extend the logic of the menu
- Inefficient because menus usually don't change, so why store the data in a database.
- JavaScript is directly related to <div> tags written out with TagLib
- Difficult to manage the menu's and the code
This menu system worked great for our group, that is until the person who wrote the menu system was not working for us anymore and we need to add new menu logic and navigation to the menu. It took not hours, wait for it, wait for it, weeks to figure out how all of the pieces worked together. Of course there was no documentation for the creation of the menus, how all the pieces worked together, or even what some of the cryptic values stored in the database were used for. This now became a maintenance nightmare for the whole group.
I then moved onto another project where again I needed a menu system to work with the system. I cringed at the thought of having to use a menu system like the one I talked about before. I figured there had to be a easier way of doing this. Well I was right. After doing about 30 minutes of research I came across Struts Menus.
Struts menus was developed to be a addin for the struts taglib's, with the ability to create css, dhtml, velocity, and other menus by storing the menu data in a xml file and then parsing the xml file with the appropriate menu filter to get the desired affect.
Struts Menus had many advantages like:
- CSS, DHTML, Javascript, ect menu's included in the package
- Easy to configure the menu's and submenus
- Easy to setup role based security with the menus
- Will work with ACEGI roles.
- Easy to extend the menus that are being created and add custom logic to the menus
- As of version 2.3 you can use the struts menus outside of the struts code
- Easily works with spring
- Easy to maintian and understand.
The struts menu system stores the information in a xml configuration file called WEB-INF/menu-config.xml. The configuration file is used to setup displayer names and associated java code to generate the menus in the page and Menus. The menus are where the menu id and menu categories are managed. Items are the menu items for each menu. You can have items nested within other items so you can have a submenu within your main menu. Also you will notice the roles value. This tells the menu system what roles can view the menu. If none are filled in then everyone can see the menu. This makes for managing the security of the menu a quick and easy task. This is a very easy xml file to configure and allows you to have a menu system generated very quickly. Below is a example of a menu-config.xml file :
<?xml version="1.0" encoding="UTF-8" ?>
<MenuConfig>
<Displayers>
<Displayer name="DropDown"
type="net.sf.navigator.displayer.DropDownMenuDisplayer"/>
</Displayers>
<Menus>
<Menu name="reports" title="REPORTS" > <!--roles="ROLE_SUPERVISOR"-->
<Item name="Accounting" title="Accounting">
<Item name="wsr" title="Weekly Status Report" width="140">
<Item name="test" parent_name="wsr" title="This is a test" />
</Item>
<Item name="Supply" title="Supply Report" width="140"/>
</Item>
<Item title="Customer Service" />
</Menu>
<Menu name="admin" title="Admin" roles="ROLE_CR_ADMIN">
<Item title="User Management" page="admin/cppManageUsers.action" width="140"/>
<Item title="CodeList Management" page="admin/cppManageCodeList.action" width="140"/>
</Menu>
</Menus>
</MenuConfig>
Now for the magic. To get the menu displayed in the jsp page all you need to do is incorporate the jsp page tag at the top of the page which will point to the taglib file struts-menu.tld.
<%@ taglib uri="/menu" prefix="menu"%>
Now you can use that tag to call the menu system, tell it which reader to use, turn on security, and tell the menu system which menus to display. A example of the tags being used are displayed below:
<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/styles/menuDropdown.css" />
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/menuDropdown.js"></script>
</head>
<body>
<menu:useMenuDisplayer name="DropDown" permission="rolesAdapter">
<menu:displayMenu name="reports"/>
<menu:displayMenu name="cpp"/>
</menu:useMenuDisplayer>
</body>
</html>
As you have probably noticed that the menu does require calls to the appropriate javascript and css stylesheets. The permission="rolesAdapter" tells the menu to use the default roles adapter configuration which will get roles setup in the container level or you can use ACEGI and this will all work great.
As you can tell that this is a pretty well written and easy to use menu navigation system that is very easy to maintain. To check out the menu system in action click here. Also go to the Struts Menus to download the jar files and read more about how to configure struts menus with struts, spring, or completely by itself.
I have also extended the struts menu displayer's so that I could preform custom actions on menus. I plan on talking about how I did that at a later time. I have found the Struts Menus project to be very useful. It has helped me quickly create professional looking menus in a matter of minutes, even with security. Go ahead and give it a try, you will probably never write another menu system again.




Good article -- I am also trying to integrate Struts Menu 2.4.3 with Spring Security. I think I have done what you have suggested here, but my role is still displaying. Do I have to do additional configuration in the applicationContext.xml. These are my current configurations:
menu-config.xml:
type="net.sf.navigator.displayer.DropDownMenuDispl ayer"/>
navigation.jsp:
Posted by Dale on February 08, 2009 at 06:49 PM EST #