记得dwr.jar要加入到WEB-INF/lib
在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>
首先写个JS要调用的类
package wsq;
import java.util.HashMap;
public class Data {
private HashMap<String, String[ ]> address;
public Data() {
address= new HashMap<String, String[]>( );
address.put("--请选择--", new String[] { "--请选择--" });
address.put("安徽", new String[] { "合肥", "安庆", "广德" });
address.put("浙江", new String[] { "杭州", "湖州" });
address.put("江苏", new String[] { "南京", "苏州", "无锡" });
address.put("江西", new String[] { "南昌" });
address.put("广东", new String[] { "广州", "韶关", "白云" });
}
public String[] getYears() {
String[] keys = new String[address.size()];
int i = 0;
for (String key : address.keySet()) {
keys[i++] = key;
}
return keys;
}
public String[] getAddress(String year) {
return address.get(year);
}
}
dwr.xml 的内容如下:
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="Address" >
<param name="class" value="wsq.Data"></param>
</create>
</allow>
</dwr>
jsp 页面
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>list.jsp</title>
<script type="text/javascript" src="/dwr_1/dwr/interface/Address.js"></script>
<script type="text/javascript" src="/dwr_1/dwr/util.js"></script>
<script type="text/javascript" src="/dwr_1/dwr/engine.js"></script>
<script type="text/javascript">
function getYears()
{
Address.getYears(callbackYear);
}
function callbackYear(data)
{
DWRUtil.addOptions("oid2",["--请选择--"]);
DWRUtil.removeAllOptions("oid1");
DWRUtil.addOptions("oid1", data);
}
function changYear()
{
var year = $("oid1").value;
Address.getAddress(year,callback1);
}
function callback1(data)
{
DWRUtil.removeAllOptions("oid2");
DWRUtil.addOptions("oid2", data);
}
</script>
</head>
<body onload="getYears()" >
<select id="oid1" onchange="changYear()" >
</select>
<select id="oid2" >
</select>
</body>
</html>
posted on 2007-09-13 13:16
Crying 阅读(1494)
评论(3) 编辑 收藏 所属分类:
DWR