Build a database-driven dynamic accordion menu in JSP using an Oracle database, Java JDBC backend, and jQuery.
Sometimes we need an Accordion Menu in our project. jQuery is one of the best options for this. Very easily we can make an Accordion Menu using jQuery and HTML. Here we will show how to make a dynamic Accordion Menu using Oracle, Java and jQuery. Whenever we need a new menu item or a new group with menu items, we enter the details into the database and it displays as a dynamic menu at runtime.
Table Structure for the Menu
Sample table structure in Oracle:
| Column | Type | Description |
|---|---|---|
| menu_item | VARCHAR2(50) | Display text of the menu item |
| link_page | VARCHAR2(100) | Target URL or page |
| tool_tip | VARCHAR2(100) | Tooltip text on hover |
| item_order | NUMBER | Order of display |
| item_type | VARCHAR2(20) | 'header' for group, 'item' for sub-item |
| item_group | VARCHAR2(50) | Parent group name for items |
Sample Database Data
Group 1: Java/J2EE
- Core Java
- JSP
- Servlets
- Struts
Group 2: Database
- Oracle
- PL/SQL
- SQL Server
In this database schema, we have two groups: Java/J2EE and Database.
The item_type 'header' represents the main group header, while 'item' represents a menu item under the group. The item_group column defines which item belongs under which group.
DynamicMenu.java
This Java class retrieves menu items from the Oracle database using JDBC and groups them into a Map<String, List> structure:
package com.codewale.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DynamicMenu {
public Map getMenuItems() {
Map menuMap = null;
Connection connection = null;
menuMap = new HashMap();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "CHRIST", "CHRIST");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(
"select menu_item from dynamic_menu where item_type='header' order by item_order");
while (rs.next()) {
Statement st1 = connection.createStatement();
String group = rs.getString("menu_item");
ResultSet rs1 = st1.executeQuery(
"select menu_item,link_page,tool_tip,item_order from dynamic_menu where item_type='item' and item_group='" + group + "' order by item_order");
List menuList = new ArrayList();
while (rs1.next()) {
Map columnMap = new HashMap();
columnMap.put("menu_item", rs1.getString("menu_item"));
columnMap.put("link_page", rs1.getString("link_page"));
columnMap.put("tool_tip", rs1.getString("tool_tip"));
menuList.add((rs1.getInt("item_order") - 1), columnMap);
}
menuMap.put(group, menuList);
}
} catch (SQLException e) {
e.printStackTrace();
}
return menuMap;
}
}Index.jsp
In Index.jsp, we query DynamicMenu and render the HTML markup alongside the jQuery accordion logic:
<%@ page import="com.codewale.example.DynamicMenu" %>
<%@ page import="java.util.Map,java.util.Set,java.util.List;" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Dynamic Accordion Menu</title>
<script type="text/javascript" src="./js/jquery-1.2.6.min.js"></script>
<script language="javascript">
function initMenu() {
$('#menu ul').hide();
$('#menu ul:first').show();
$('#menu li a').click(
function() {
var checkElement = $(this).next();
if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
return false;
}
if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#menu ul:visible').slideUp('normal');
checkElement.slideDown('normal');
return false;
}
}
);
}
$(document).ready(function() { initMenu(); });
</script>
<style type="text/css">
div.menu {
background-color: #ffffff;
margin-bottom: 5px;
border: 1px solid #9EE3FF;
}
ul#menu, ul#menu ul {
list-style-type: none;
margin: 0;
padding: 0;
}
ul#menu a {
display: block;
text-decoration: none;
}
ul#menu li {
margin-top: 1px;
border-top: 1px dotted #D9F2FC;
}
ul#menu li a {
background: #D3EFFB;
color: #03597D;
padding: 0.5em;
}
ul#menu li a:hover {
background: #C8EBFA;
}
ul#menu li ul li a {
background: #FFFFFF;
color: #03597D;
}
ul#menu li ul li a:hover {
background: #F7FCFE;
border-left: 5px #03597D solid;
}
</style>
</head>
<body>
<table width="20%">
<tr><td>
<%
try {
DynamicMenu dm = new DynamicMenu();
Map menuMap = dm.getMenuItems();
Set<String> keys = menuMap.keySet();
out.print("<ul id=\"menu\">"); // start of menu
for (String key : keys) {
out.print("<li>");
out.print("<a href=\"#\">" + key + "</a>");
out.print("<ul>");
List itemList = (List) menuMap.get(key);
for (int i = 0; i < itemList.size(); i++) {
Map columnMap = (Map) itemList.get(i);
String menu_item = columnMap.get("menu_item").toString();
String link_page = columnMap.get("link_page").toString();
String tool_tip = columnMap.get("tool_tip").toString();
out.print("<li><a href=\"" + link_page + "\" title=\"" + tool_tip + "\">" + menu_item + "</a></li>");
}
out.print("</ul></li>");
}
out.print("</ul>"); // end of menu
} catch (Exception e) {
out.print(e);
}
%>
</td></tr>
</table>
</body>
</html>How It Works
- Database retrieval: The
DynamicMenuclass queries all header records and retrieves child menu items based on theitem_group. - JSP rendering: The JSP iterates over the key set of groups and outputs nested unordered lists (
<ul id="menu">). - jQuery slide animation: The
initMenuscript intercepts clicks on group anchors and slides open the child<ul>while closing any currently visible group.