What Sothink DHTML Menu Can Do for You?

 

  Back Home Next


Donwload Sothink DHTML Menu

JSP sample of dynamic menu created from database

This sample is created by our honour user, Julio Antonio Reguero Hernández.
Thanks for all effort and support from him!

Part 1: Sample Instruction

To create a dynamic menu from database, you can input some condition clauses using either CGI (JSP, PHP, JSP, and etc.) or JavaScript language, that is, to input the Prefix or Suffix for a menu item in Menu Item > Condition.

All the files to a web server that supports JSP (JavaServer Pages) so you can view it.

or it

As you see, in this sample, the menu contains two levels: the parent level showing the product categories and the sub level showing the specific products. Actually, there are only two menu items, one in the parent level and the other is in the sub level.
The final result that you are seeing is just got from database.

Below is the diagram of the database.

 

Back to top

Part 2: Steps

The following are the steps of this example:
Before creating the menu, you should make a database containing the information of the product categories and the specific products.
To do this follow one of two steps below
a) Create a database named "dmenu" in your mySQL, create a database user named "dbuser" and the password is "123". By default you can use the user named "root" and the password "". Then you can use the following code to generate tables:
CREATE TABLE Category (
CID int(11) NOT NULL auto_increment,
Name char(255) DEFAULT '' NOT NULL,
Notes char(255),
PRIMARY KEY (CID)
);

CREATE TABLE Product (
PID int(11) NOT NULL auto_increment,
CID int(11),
Name char(255) DEFAULT '' NOT NULL,
Link char(255),
Notes char(255),
PRIMARY KEY (PID)
);

Then you can add a few records to the tables.

b) Copy and paste the content of folder db_mysql include in the JSP sample package into C:\mysql\data assuming this is your path to MySQL directory installation.

2. Create a new menu. Click "General" in "Menu Item", select "HTML". Input <%=sCatName%> in the context box.

3. Click "Condition" in "Menu Item".

4. In "Menu Item > Condition" Dialog, input the prefix and suffix for the menu item.

Note: when you add the condition, you may get a warning message in DHTML Menu. That's because the condition code is server-side code which can not be processed by the browser directly. The warning will not show when you add the menu to an JSP page that is supported by server. You can uncheck "Preview > Enable Auto-Refresh" to avoid seeing the warnings.
Tips: To prevent this error popping up, you can add the JavaScript comment delimiters /* and */ to the Prefix as well as Suffix. In that way the preview ignores the extra code and the code works well on the server side processing.

For example:
Prefix:
/*
<%
while (Recordset_SelectCatName.next())
{
sCatName = Recordset_SelectCatName.getString("Name").trim();
sCatCod = Recordset_SelectCatName.getString("CID").trim();
%>
*/

Suffix:
/*
<%
}
Recordset_SelectCatName.close();
StatementRecordset_SelectCatName.close();
ConnRecordset_SelectCatName.close();
%>
*/

5. Click "Add" button to add a sub item of it. Change the item type to "HTML", then input <%=sProductName%>. In the link field, input <%=sProductLink%>.

6. Click "Condition" in "Menu Item" and input the prefix and suffix for the menu item.

Prefix
<%
Driver DriverRecordset_SelectProductName = (Driver)Class.forName(DB_DRIVER).newInstance();
Connection ConnRecordset_SelectProductName = DriverManager.getConnection(DB_STRING,DB_USERNAME,DB_PASSWORD);
PreparedStatement StatementRecordset_SelectProductName = ConnRecordset_SelectProductName.prepareStatement("select * from Product where CID="+sCatCod);
ResultSet Recordset_SelectProductName = StatementRecordset_SelectProductName.executeQuery();

int Recordset_SelectProductName_numRows = 0;
while (Recordset_SelectProductName.next())
{
sProductName = Recordset_SelectProductName.getString("Name").trim();
sProductLink = Recordset_SelectProductName.getString("Link").trim();
%>

Suffix
<%
}
Recordset_SelectProductName.close();
StatementRecordset_SelectProductName.close();
ConnRecordset_SelectProductName.close();
%>

Note: Normally in order to optimize the menu code, if several menu items have the same property, only one item's property code is recorded and other items take reference of its property code. If we add a condition to this item, there is possibility that it will be invisible when its condition is not met. And its property code can not be referenced by other items either. This will cause the whole menu can not work correctly. Please do check "This item may be invisible at run-time" to avoid the problem.

Now you can Click the button "Publish" to choose any option to insert the menu into your JSP page and also publish all the relevant resource files.

Back to top

Part 3: Code Explanation

This is the source code at the beginning of this sample page.

Code

Explanation

<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.lang.*"%>  Library's declaration
<% String DB_DRIVER = "com.mysql.jdbc.Driver";
String DB_USERNAME = "dbuser";
String DB_PASSWORD = "123";
String DB_STRING = "jdbc:mysql://localhost:3306/dmenu";
String sCatName = "";
String sCatCod = "";
String sProductName = "";
String sProductLink = "";

Driver DriverRecordset_SelectCatName = (Driver)Class.forName(DB_DRIVER).newInstance();
Connection ConnRecordset_SelectCatName = DriverManager.getConnection(DB_STRING,DB_USERNAME,DB_PASSWORD);
Connect with the database.
PreparedStatement StatementRecordset_SelectCatName = ConnRecordset_SelectCatName.prepareStatement(
"select * from Category");
ResultSet Recordset_SelectCatName = StatementRecordset_SelectCatName.executeQuery();
int Recordset_SelectCatName_numRows = 0;
%>
 
Retrieve recordset from the CATEGORY table.

After you add item condition and edit the menu item, DHTML Menu inserts following code into the page. The entire source code below is generated by DHTML Menu program. Your editing of conditions will be saved in the menu's configuration file (*.pgt), which can be edited again.
To learn further what the code represents,  please read the following code explanation:

Code

Explanation

stm_bm(["uueoehr",400,"".....) Begin the whole menu.
    stm_bp("p0",[0,4,0,0,3,1,0,0,100,"",-2...... ) Begin a popup menu (items of category).
<%
while (Recordset_SelectCatName.next())
{
sCatName = Recordset_SelectCatName.getString("Name").trim();
sCatCod = Recordset_SelectCatName.getString("CID").trim();
%>
Walk through the CATEGORY recordset and save each category's name as variable "sCatName".
         stm_ai("p0i0",[1,"<%=sCatName%>","",....) <%=sCatName%> is the category name.
         stm_bpx("p1","p0",[1,4,0,-1,.....) Begin a popup menu (items of product).
<% Driver DriverRecordset_SelectProductName = (Driver)Class.forName(DB_DRIVER).newInstance();
Connection ConnRecordset_SelectProductName = DriverManager.getConnection(
DB_STRING,DB_USERNAME,DB_PASSWORD);
PreparedStatement StatementRecordset_SelectProductName =
ConnRecordset_SelectProductName.prepareStatement(
"select * from Product where CID="+sCatCod);
ResultSet Recordset_SelectProductName = StatementRecordset_SelectProductName.executeQuery();
int Recordset_SelectProductName_numRows = 0;
while (Recordset_SelectProductName.next())
{
sProductName = Recordset_SelectProductName.getString("Name").trim();
sProductLink = Recordset_SelectProductName.getString("Link").trim();
%>

Retrieve recordset from the PRODUCT table. Walk through the PRODUCT recordset and save product name and link as temporary variables.

         stm_ai("p1i0",[1,"<%=sProductName%> ......"<%=sProductLink%>...) Append an item. <%=sProductName%> is the item text.

<%} Recordset_SelectProductName.close();
StatementRecordset_SelectProductName.close();
ConnRecordset_SelectProductName.close();%>

Move to the next record of PRODUCT table.
   stm_ep() End of a popup menu.

<%} Recordset_SelectCatName.close();
StatementRecordset_SelectCatName.close();
ConnRecordset_SelectCatName.close();%>

Move to the next record of the CATEGORY recordset.

   stm_ep() End of a popup menu.
stm_em() End of the whole menu.

 

Back to top

Donwload Sothink DHTML Menu

Home

1234