疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

dwr实现级联菜单

Posted on 2007-12-08 10:39 疯狂 阅读(2388) 评论(2)  编辑  收藏
 

Dwr的介绍请见:http://www.blogjava.net/freeman1984/archive/2007/11/25/163010.html

本例将实现一个利用dwr实现的级联菜单处理:

首先建立两个实体类:Org(机构)Person(机构下的人员)

代码如下:

Org.java

package com.dwr;

publicclass Org {

    privateintid;

    private String name;

   

    publicint getId() {

       returnid;

    }

    publicvoid setId(int id) {

       this.id = id;

    }

    public String getName() {

       returnname;

    }

    publicvoid setName(String name) {

       this.name = name;

    }

}

Person.java

package com.dwr;

publicclass Person {

    privateintid;

    private String name;

   

    publicint getId() {

       returnid;

    }

    publicvoid setId(int id) {

       this.id = id;

    }

    public String getName() {

       returnname;

    }

    publicvoid setName(String name) {

       this.name = name;

    }

}

创建管理类:PersonManager用于初始化数据和返回数据

package com.dwr;

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class PersonManager {

   

    private static Map orgs = new HashMap();

    private static Map persons = new HashMap();

    //初始化数据

    static{

       for(int i=0; i<10; i++){

           Org org = new Org();

           org.setId(i);

           org.setName("机构"+i);

           orgs.put(org.getId(), org);

          

           List personsList = new ArrayList();

           for(int j=0; j < 20; j++){

              Person person = new Person();

              person.setId(j);

              person.setName("org["+org.getName()+"]下的人员[人员"+j+"]");

              personsList.add(person);

           }

           persons.put(org.getId(), personsList);

       }

    }

    //返回所有机构

    public Collection searchAllOrg(){

       return orgs.values();

    }

    //返回某机构所有人员

    public List searchPersons(int orgId){

       return (List)persons.get(orgId);

    }

}

配置文件:

Web.xml中加入以下代码:

<servlet>

    <servlet-name>dwr-invoker</servlet-name>

    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

    <init-param>

      <param-name>debug</param-name>

      <param-value>true</param-value>

    </init-param>

 </servlet>

 <servlet-mapping>

    <servlet-name>dwr-invoker</servlet-name>

    <url-pattern>/dwr/*</url-pattern>

 </servlet-mapping>

添加dwr.xmlweb.xml同一个目录

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>

 <allow>

     <create creator="new" javascript="personManager">

      <param name="class" value="com.dwr.PersonManager"/>

    </create>

    <convert converter="bean" match="com.dwr.Org"/>

    <convert converter="bean" match="com.dwr.Person"/>

 </allow>

</dwr>

测试页面:

<%@ page language="java" contentType="text/html; charset=GB18030"

    pageEncoding="GB18030"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">

<script type="text/javascript" src="dwr/engine.js"></script>

<script type="text/javascript" src="dwr/util.js"></script>

<script type="text/javascript" src="dwr/interface/personManager.js"></script>

<title>Insert title here</title>

<script type="text/javascript">

function init(){

    personManager.searchAllOrg(

       function(data){

           //dataOrg对象的集合,id,name

           dwr.util.removeAllOptions("o");

           dwr.util.addOptions("o",data,"id","name");

           searchPersons();

       }

    );

}

function searchPersons(){

    personManager.searchPersons(

       $("o").value,

       function(data){

           dwr.util.removeAllOptions("p");

           dwr.util.addOptions("p",data,"id","name");

       }

    );

}

</script>

</head>

<body onload="init()">

请选择机构信息:<select id="o" onchange="searchPersons()">

</select>

请选择人员信息:<select id="p">

</select>

</body>

</html>


评论

# re: dwr实现级联菜单  回复  更多评论   

2013-05-31 09:37 by 沈震
想问下,我的dwr+ssh级联能出来一级的,二级的出不来,callback不能使用,一直报错error
我写的级联是 院系, 专业二级级联!,求指导!!

# re: dwr实现级联菜单  回复  更多评论   

2013-05-31 09:38 by 沈震
我的邮箱1692894352@qq.com

只有注册用户登录后才能发表评论。


网站导航: