nv.jsp 使用javascript將下級菜單顯示在頁面上 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'nv.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type='text/javascript' src='/DwrTest/dwr/engine.js'></script> <script type='text/javascript' src='/DwrTest/dwr/interface/menuDao.js'></script> <script type='text/javascript' src='/DwrTest/dwr/util.js'></script> <script type="text/javascript"> var id; var n; function callBack(data) { if(data.length>0){ var nvs=document.getElementById(id); if(nvs.innerHTML.indexOf("DIV")>-1 || nvs.innerHTML.indexOf("div")>-1){ var divch=nvs.lastChild; if(divch.style.display=='none'){ divch.style.display='block'; }else { divch.style.display='none'; } }else{ var nvstr="<div>"+nvs.innerHTML+"</div>"; var spn=""; for(var j=0;j<n*2;j++){ spn=spn+" "; } n=n+1; nvstr=nvstr+"<div>"; for(var i=0;i<data.length;i++){ nvstr=nvstr+"<div id='"+data[i].id+"'>"+spn+"<a href='javascript:void(0)' onclick='nvsClick("+data[i].id+","+n+")' >"+data[i].menuname+"</a></div>"; } nvs.innerHTML=nvstr+"</div>"; } } } function nvsClick(nvid,nu){ id=nvid; n=nu; menuDao.himenuDao(id,callBack); } </script> </head> <body> <div> <div id="0" style="display: block"> <a onclick="nvsClick(0,1)" href="javascript:void(0)">主菜單</a> </div> </div> </body> </html> dwr.xml配置文件 配置好要使用的實現ajax類的類以及bean. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://www.getahead.ltd.uk/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="service"> <param name="class" value="dwr.Service" /> </create> <create creator="new" javascript="menuDao"> <param name="class" value="dwr.MenuDao" /> </create> <convert match="bean.User" converter="bean"> <param name="include" value="id,username,password"></param> </convert> <convert match="bean.Himenu" converter="bean"> <param name="include" value="id,menuname,displayref,parentMenu,systemClass,menuOreder"></param> </convert> </allow> </dwr> ajax實現類,讓dwr生成的函數從這裡獲得菜單數據 package dwr; import java.util.ArrayList; import java.util.List; import bean.Himenu; public class MenuDao { private List<Himenu> list=new ArrayList<Himenu>(); public MenuDao(){ Himenu h=null; int k=1; for(int i=0;i<10;i++){ for(int j=1;j<10;j++){ h=new Himenu(k,"菜單"+i+""+j,"菜單"+i+"j",i,1,i); list.add(h); k++; } } } public List<Himenu> himenuDao(int id){ List<Himenu> l=new ArrayList<Himenu>(); Himenu h=null; for(int i=1;i<list.size();i++){ h=list.get(i); if(h.getParentMenu()==id){ l.add(h); } } return l; } } 菜單 bean public class Himenu { /* * 菜單ID */ private int id; /* * 菜單名稱 */ private String menuname; /* * 菜單引用描述 */ private String displayref; /* * 父節點 */ private int parentMenu; /* * 所屬子系統ID */ private int systemClass; /* * 排序字段 */ private int menuOreder; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMenuname() { return menuname; } public void setMenuname(String menuname) { this.menuname = menuname; } public String getDisplayref() { return displayref; } public void setDisplayref(String displayref) { this.displayref = displayref; } public int getParentMenu() { return parentMenu; } public void setParentMenu(int parentMenu) { this.parentMenu = parentMenu; } public int getSystemClass() { return systemClass; } public void setSystemClass(int systemClass) { this.systemClass = systemClass; } public int getMenuOreder() { return menuOreder; } public void setMenuOreder(int menuOreder) { this.menuOreder = menuOreder; }www.2cto.com public Himenu (){} public Himenu(int id, String menuname, String displayref, int parentMenu, int systemClass, int menuOreder) { super(); this.id = id; this.menuname = menuname; this.displayref = displayref; this.parentMenu = parentMenu; this.systemClass = systemClass; this.menuOreder = menuOreder; } }