jsp中来自数据库的动态菜单 [英] Dynamic Menu from database in jsp

查看:77
本文介绍了jsp中来自数据库的动态菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..



任何人都可以帮我写一个动态菜单生成代码,从oracle或jsp中的任何数据库生成???

解决方案

它在这里不起作用。



以下是对询问者的期望:

1. 首先尝试你想做什么!你可能会发现它并不那么难。

2.制定你所做的看似问题/不工作的事情。



尝试一下,告诉你是否面临问题。

各位会员非常乐意为此提供帮助。






一个类似的问题,几乎无法启动:在JSP或javascript中动态填充数据库中的菜单项 [ ^ ]


有时我们需要根据我们的要求生成菜单表单数据库。以下网址



[ ^ ]将帮助您在ASP.NET中动态生成菜单。您可以根据需要应用自定义CSS,以提供菜单的美观。


我的jsp页面是:



< pre lang =text><![CDATA [<%@ page contentType =text / htmlpageEncoding =UTF-8%>]]>
<![CDATA [<%@ page language =javaimport =java.util。*%>]]>
<![CDATA [<%@ page language =javaimport =java.sql。*%>]]>


< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< link rel =stylesheettype =text / csshref =skin.css>
< title> JSP页< / title>
< / head>
< body>
< div>
<%
ArrayList list = new ArrayList();
ArrayList sublist = new ArrayList();
try {
Class.forName(oracle.jdbc.OracleDriver);
Connection con = DriverManager.getConnection(jdbc:oracle:thin:@ // localhost:1521 / ORCL,PORDFW,PORDFW);
%>
< ul>
<%
String query1 =select * from tbl_category;
语句st1 = con.createStatement();
ResultSet rs1 = st1.executeQuery(query1);

while(rs1.next()){


%>
< li>
< a href =#>

<%= rs1.getString(category_name)%>
< / a>
< ul>
<%
字符串query2 =从tbl_subcategory中选择subcategory_name,其中category_id ='+ rs1.getString(category_id)+';
语句st2 = con.createStatement();
ResultSet rs2 = st1.executeQuery(query2);
while(rs2.next()){
%>
< li>
< a href =#>
<%= rs2.getString(subcategory_name)%>
< / a>
< / li>
<%
}
%>
< / ul>
< / li>
<%
}
%>
< / ul>
<%
} catch(例外e1){
}
%>
< / div>
< / body>
< / html>



< / link>







我的数据库有2个表:



1. tbl_category - > category_id,category_name

2. tbl_subcategory - > subcategory_id,category_id,subcategory_name



第一个表有2个值为

Category_id Category_name

----- ------- --------------

1体育

2书籍



第二个表值,

子类别_id Category_id子类别名称

-------------- ------ ------ -----------------

1 1 Cricket

2 1足球

3 2科学

4 2数学



我的CSS:

ul {

font-family:Arial,Verdan a;

字体大小:14px;

保证金:0;

填充:0;

list-风格:无;

}

ul li {

display:block;

position:relative;

浮动:左;

}

li ul {

显示:无;

}

ul li a {

display:block;

text-decoration:none;

color:# ffffff;

border-top:1px solid #ffffff;

填充:5px 15px 5px 15px;

背景:#2C5463;

margin-left:1px;

white-space:nowrap;

}

ul li a:hover {background: #617F8A; }

li:hover ul {

display:block;

position:absolute;

}

li:hover li {

float:none;

font-size:14px;

}

li:悬停{background:#617F8A; }

li:hover li a:hover {background:#95A9B1; }

ul li.next {

position:absolute;

display:block;

float:left;

背景:#E8E8E8;

字体大小:14px;

border-top:1px solid #ffffff;

填充:5px 15px 5px 15px;

}

ul li a.first {

背景:#617F8A;

}





当我运行这个jsp时,我得到的菜单为:



体育

>板球

>足球



我没有第二个菜单书籍及其子菜单....





请帮帮我...



提前致谢..


Hi..

Can anyone pls help me to write a code for the dynamic menu generation from oracle or any database in jsp???

解决方案

It does not work like this here.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.



A similar question with few headsup to start: Dynamically populating menu items from the database in JSP or javascript[^]


Sometimes we need to generate menu form database according to our requirements. The following url

[^] will help you to generate menu dynamically in ASP.NET. You can apply your customize CSS as you like to give nice outlook of the menu.


My jsp page is:

<![CDATA[<%@page contentType="text/html" pageEncoding="UTF-8"%>]]>
<![CDATA[<%@page language="java" import="java.util.*" %>]]>
<![CDATA[<%@page language="java" import="java.sql.*" %>]]>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="skin.css">
        <title>JSP Page</title>
    </head>
    <body>
        <div>
            <%
                ArrayList list = new ArrayList();
                ArrayList sublist = new ArrayList();
                try {
                    Class.forName("oracle.jdbc.OracleDriver");  
                    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/ORCL", "PORDFW", "PORDFW");
            %>
            <ul>
                <%
                    String query1 = "select * from tbl_category";
                    Statement st1 = con.createStatement();
                    ResultSet rs1 = st1.executeQuery(query1);
                    
                    while (rs1.next()) {
                        

                %>
                <li>
                    <a href="#">

                        <%= rs1.getString("category_name")%>
                    </a>
                    <ul>
                        <%
                            String query2 = "select subcategory_name from tbl_subcategory where category_id='" + rs1.getString("category_id") + "'";
                            Statement st2 = con.createStatement();
                            ResultSet rs2 = st1.executeQuery(query2);
                            while (rs2.next()) {
                        %>
                        <li>
                            <a href="#">
                                <%= rs2.getString("subcategory_name")%>
                            </a>
                        </li>
                        <%
                            }
                        %>
                    </ul>
                </li>
                <%
                    }
                %>
            </ul>
            <%
                } catch (Exception e1) {
                }
            %>
        </div>  
    </body>
</html>



</link>




my database has 2 tables:

1. tbl_category - > category_id, category_name
2. tbl_subcategory - > subcategory_id, category_id, subcategory_name

First table has 2 values as
Category_id Category_name
------------ --------------
1 Sports
2 Books

Second table values,
Subcategory_id Category_id Subcategory_name
-------------- ------------ -----------------
1 1 Cricket
2 1 Football
3 2 Science
4 2 Maths

My CSS :
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #2C5463;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 14px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }
ul li.next {
position: absolute;
display: block;
float: left;
background:#E8E8E8;
font-size: 14px;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
}
ul li a.first {
background: #617F8A;
}


When i run this jsp, i am getting the menu as:

Sports
> Cricket
> Footbal

I am not getting the second menu "Books" with its submenu....


Pls help me...

Thanks in advance..


这篇关于jsp中来自数据库的动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆