Servlet动态菜单

功能:
从Oracle数据库查询类型及链接、动态显示在桌面

Dao层

public class CheckMenuDao {
    private static String DRIVER_NAME="oracle.jdbc.driver.OracleDriver";
    private static String DATABASE_URL="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    private static String USER_NAME="scott";
    private static String PASSWORD="Oracle11";  

    public static List findMenu(){
        List menuList=new ArrayList();
        Connection conn=null;
        CallableStatement cstmt=null;
        ResultSet rs=null;
        System.out.println("连接数据库.....");
        try {
            Class.forName(DRIVER_NAME);
            conn=DriverManager.getConnection(DATABASE_URL, USER_NAME, PASSWORD);
            PreparedStatement ps=conn.prepareStatement("select name,url from catagory order by id");
            rs=ps.executeQuery();

            while(rs.next()){
                Menus menu=new Menus();
                menu.setMenuName(rs.getString(1));
                menu.setMenuUrl(rs.getString(2));
                menuList.add(menu);
            }
        } catch (ClassNotFoundException e) {
            System.out.println("");
        } catch (SQLException e) {
            System.out.println("");
        }finally{
            try {
                if(rs!=null)
                    rs.close();
                if(cstmt!=null)
                    cstmt.close();
                if(conn!=null)
                    conn.close();
            } catch (SQLException e) {
                System.out.println("");
            }
        }
        return menuList;
    }   
}

entity

public class Menus {
    private String menuName="";
    private String menuUrl="";
    //setter\getter
}

Servlet

public class ShowMenu extends HttpServlet{
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List list=CheckMenuDao.findMenu();
        request.setAttribute("Menu", list);
        //转发到left.jsp
        request.getRequestDispatcher("left.jsp").forward(request, response);
    }
}

页面布局:

left.html:

    <%@ page language="java" import="java.util.*,dynamic.menu.entity.*" pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>标题</title>
<style type="text/css">
<!--
.STYLE1 {
    font-size: 16px;
    color: #FF9933;
    font-weight: bold;
}
.STYLE6 {font-size: 14px; color: #FFFFFF; }
a:link {
    color: #FFFFFF;
}
a:visited {
    color: #FFFFFF;
}
a:hover {
    color: #FF9933;
}
a:active {
    color: #9900CC;
}
-->
</style>
</head>
<%
List list=(List)request.getAttribute("Menu");
if(list==null || list.size()==0 ){
list=new ArrayList();
list.add(new Menus());
}
%>
<body topMargin="5px" >
<table width="100%"  border="0" cellpadding="5" cellspacing="1" bgcolor="#ff9900">
  <tr bgcolor="#cccccc">
    <td height="15" align="left" valign="bottom" bgcolor="#cccccc"><span class="STYLE1">显示</span></td>
  </tr>
  <%
  for(int i=0;i<list.size();i++){
  Menus menu=(Menus)list.get(i);
   %>
  <tr bgcolor="#999999">
    <td height="12" align="center" bgcolor="#999999" class="STYLE6"><a href="<%=path+"/"+menu.getMenuUrl()+".html" %>" target="mainFrame"><%=menu.getMenuName() %></a></td>
  </tr>
  <%} %>
</table>
</body>
</html>

right:

    <style type="text/css">
<!--
.STYLE2 {font-size: 36px}
.STYLE4 {
    font-size: 15px;
    font-weight: bold;
}
.STYLE5 {
    font-size: 15px;
    color: #00FF00;
}
-->
</style>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" align="center" valign="bottom">
        <span class="STYLE2">welcome</span>
    </td>
  </tr>
  <tr>
    <td height="50" align="center" valign="top">
        <span class="STYLE4">welcome to Test Dynamic Menu</span>
    </td>
  </tr>

  <tr>
     <td height="50" align="center">菜单
        <select name="grant"  onchange="parent.document.all.leftFrame.src='grant?grant=' + this.value"/> 
          <option value="0" selected>请选择</option>
          <option value="1" >1</option>
          <option value="2" >2</option>
        </select>
     </td>
  </tr>
</table>

index.html:

<frameset rows="*,120" cols="*" frameborder="no" border="0" framespacing="0">
    <frameset cols="180,*" frameborder="no" border="0" framespacing="0">
        <frame src="left.jsp" name="leftFrame"  noresize="noresize" marginwidth="0" marginheight="0" id="leftFrame" title="leftFrame" />
        <frame src="main.html" name="mainFrame" marginwidth="0" marginheight="0" id="mainFrame" title="mainFrame" />
    </frameset>
</frameset>
<noframes>

猜你喜欢

转载自blog.csdn.net/linkingfei/article/details/81503182