dwr框架实现多级菜单
使用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("p")>-1){
var divch=nvs.lastChild;
if(pch.style.display=='none'){
pch.style.display='block';
}else
{
pch.style.display='none';
}
}else{
var nvstr="<p>"+nvs.innerHTML+"</p>";
var spn="";
for(var j=0;j<n*2;j++){
spn=spn+" ";
}
n=n+1;
nvstr=nvstr+"<p>";
for(var i=0;i<data.length;i++){
nvstr=nvstr+"<p id='"+data[i].id+"'>"+spn+"<a href='javascript:void(0)' onclick='nvsClick("+data[i].id+","+n+")' >"+data[i].menuname+"</a></p>";
}
nvs.innerHTML=nvstr+"</p>";
}
}
}
function nvsClick(nvid,nu){
id=nvid;
n=nu;
menuDao.himenuDao(id,callBack);
}
</script>
</head>
<body>
<p>
<p id="0" style="display: block">
<a onclick="nvsClick(0,1)" href="javascript:void(0)">主菜单</a>
</p>
</p>
</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"
"https://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;
}
}
上一篇: 浅析黑帽seo与白帽seo的区别