1.导入dwr.jar
2.web.xml 配置dwr的servlet
<display-name> ajaxDWR</display-name>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<description></description>
<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>
3.编写类用来实现功能
package com.vivenwei;
import java.util.ResourceBundle;
public class Sayhello {
public String syshello(String name){
return name+",你好啊!";
}
//根据key值返回
public String getmsg(String key){
ResourceBundle resourceBundle = ResourceBundle.getBundle("test");
return resourceBundle.getString(key);
}
//级联菜单
public String[] getoptions(String keys){
String[] a = new String[]{"aa.12","ab.13","ac.14","ad.15"};
String[] b = new String[]{"ba.22","bb.23","bc.24","dd.25"};
String[] c = new String[]{"ca.32","cb.33","cc.34","cd.35"};
if("a".equals(keys)){
return a;
}else if("b".equals(keys)){
return b;
}
return c;
}
}
4.dwr.xml 配置,放在web.xml一个目录下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create javascript="Sayhello" creator="new"> <!--Sayhello被游览器创建出来的对象的名字 -->
<param name="class" value = "com.vivenwei.Sayhello"/><!--请求的类的路径-->
</create>
</allow>
</dwr>
5.调用类的方法
function sm1(msg) {
Sayhello.getoptions(msg.value,cleardate1);//Sayhello是游览器创建出来的对象的名字,在dwr.xml中,getoptions是类的方法,cleardate1是处理返回数据的方法;
}
6.处理返回的数据
function cleardate1(list){
var dd = $("#dd");
dd[0].length = 0;
for(var i = 0;i<list.length;i++){
var temp = list[i];
var arr = temp.split(".");
var optiona = new Option(arr[0],arr[1]);
dd[0].options.add(optiona);
}
}
============================================================
执行完一次操作
============================================================