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.xml和web.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){
//data是Org对象的集合,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>