1、首先下载dwr.jar包,放到项目的lib文件夹下。
2、在web.xml中的web-app标签中添加如下标签
<display-name>dwrtest</display-name>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
3、在WEB-INF文件夹下添加dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr
PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
"http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create creator="new" javascript="test">
<param name="class" value="test.Test"></param>
</create>
</allow>
</dwr>
4、页面中添加的js为
<script type="text/javascript" src="/mytest/dwr/interface/test.js"></script>
<script type="text/javascript" src="/mytest/dwr/engine.js"></script>
<script type="text/javascript" src="/mytest/dwr/util.js"></script>
前两个必须添加,后一个为我们使用方法提供方便。
function useselect(){
test.useSelect(disSelect);
}
function disSelect(data){
dwr.util.addOptions("select",data);//select为页面标签id属性
}
其中test.Test代码为:
public Map<String,String> useSelect(){
Map<String,String> map = new HashMap<String,String>();
map.put("1", "select1");
map.put("2", "select2");
map.put("3", "select3");
return map;
}