DWR现在已经在java平台的AJAX应用中使用比较广泛,下面将以前项目中用到的部分内容(测试部分)贴出来,以供参考。
配置使用什么的就不多说了,在网上搜一下就很多,非常简单。
只简单的说一下流程,然后直接把文件中的内容贴出来。具体的使用也可以参考dwr的网站:http://directwebremoting.org/dwr
1.首先在web.xml中配置dwr,以便在项目中使用
2.编写java应用,并在dwr.xml中配置(我的dwr.xml在web-info下)。
3.在jsp文件中调用
例如:
TestDwr.java中有一个方法public String test2(); 如果想在jsp页面中通过ajax方式调用步骤如下:
在dwr.xml中将方法公布出来
- <!--测试,调用方法-->
- <createjavascript="testdwr"creator="new">
- <paramname="class"value="com.yinbo.umpay.test.TestDwr"/>
- <includemethod="test1"/>
- <includemethod="test2"/>
- <includemethod="test3"/>
- <includemethod="test4"/>
- <includemethod="test5"/>
- <includemethod="test6"/>
- <includemethod="test7"/>
- <includemethod="test8"/>
- <includemethod="test9"/>
- </create>
javascript="testdwr"是指会生成testdwr.js的文件供前台调用。value="com.yinbo.umpay.test.TestDwr"是指方法所在的类,creator="new"是构造方法,这里使用new对象的方法,也可以通过spring来管理。这里注意即使用在TestDwr.java中是public方法,也要在配置文件中发布出来才可以访问。
在jsp中调用如下:
先要引入
<script src='<c:url value="/dwr/engine.js"/>' ></script>
<script src='<c:url value="/dwr/util.js"/>' ></script>
这两个js是支持库,必须引入。然后就是引入
<script src='<c:url value="/dwr/interface/testdwr.js"/>'></script>
这个testdwr.js是dwr动态生成的,你不用去寻找它放在哪儿。
jsp中的调用
function callTestMethod2(){
testdwr.test2(callBackFortestMethod2);
}
function callBackFortestMethod2(data){
// 其中 date 接收方法的返回值
// 可以在这里对返回值进行处理和显示等等
alert("the return value is " + data);
}
<input type="button" οnclick="callTestMethod2();"
value="调用有简单返回值的java方法">
这里的调用使用testdwr这个对象引用方法。testdwr.test2(callBackFortestMethod2); 返回值会传入callBackFortestMethod2方法的参数中。
web.xml
- <!--dwr-->
- <servlet>
- <servlet-name>dwr-invoker</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-invoker</servlet-name>
- <url-pattern>/dwr/*</url-pattern>
- </servlet-mapping>
dwr.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting2.0//EN""http://getahead.ltd.uk/dwr/dwr20.dtd">
- <dwr>
- <allow>
- <!--umpaydwrmethods-->
- <createjavascript="dwrFactory"creator="spring">
- <paramname="beanName"value="DwrFactoryImpl"/>
- <includemethod="testMethod"/>
- <includemethod="getRoleList"/>
- <includemethod="checkDetealPwd"/>
- <includemethod="sendSelectBank"/>
- <includemethod="checkBankaccountId"/>
- <includemethod="checkBankinterfaceId"/>
- <includemethod="getAccountList"/>
- <includemethod="checkUsername"/>
- <includemethod="checkwwwsite"/>
- <includemethod="checkAppIllegal"/>
- </create>
- <!--javabean返回值及参数转换-->
- <convertconverter="bean"
- match="com.yinbo.umpay.core.po.Roleinfo">
- <paramname="include"value="roleid,rolename"/>
- </convert>
- <!--测试,调用方法-->
- <createjavascript="testdwr"creator="new">
- <paramname="class"value="com.yinbo.umpay.test.TestDwr"/>
- <includemethod="test1"/>
- <includemethod="test2"/>
- <includemethod="test3"/>
- <includemethod="test4"/>
- <includemethod="test5"/>
- <includemethod="test6"/>
- <includemethod="test7"/>
- <includemethod="test8"/>
- <includemethod="test9"/>
- </create>
- <!--javabean返回值及参数转换-->
- <convertconverter="bean"match="com.yinbo.umpay.core.po.Userinfo">
- <paramname="include"value="userid,username"/>
- </convert>
- <!--spring生成-->
- <createjavascript="testdwr2"creator="spring">
- <paramname="beanName"value="TestDwrImpl"/>
- <includemethod="test2"/>
- </create>
- </allow>
- <!--List,Set,Map作参数时,声明包含的确切类-->
- <signatures>
- <![CDATA[
- importjava.util.List;
- importcom.yinbo.umpay.test.TestDwr;
- importcom.yinbo.umpay.core.po.Userinfo;
- TestDwr.test7(List<Userinfo>);
- TestDwr.test9(Map<String,Userinfo>);
- ]]>
- </signatures>
- </dwr>
TestDwr.java
- packagecom.yinbo.umpay.test;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importcom.yinbo.umpay.core.po.*;
- publicclassTestDwr{
- publicvoidtest1(){
- try{
- Thread.sleep(5000);
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- System.out.println("dwrtest1.");
- }
- publicStringtest2(){
- return"dwrtest2.";
- }
- publicStringtest3(Stringdata,Stringdata2){
- System.out.println(data);
- System.out.println(data2);
- returndata+"return"+data2;
- }
- publicUserinfotest4(){
- Userinfou=newUserinfo();
- u.setUserid("sec.peng");
- u.setUsername("pengyuanfeng");
- returnu;
- }
- publicvoidtest5(Userinfouserinfo){
- System.out.println(userinfo.getUserid());
- System.out.println(userinfo.getUsername());
- }
- publicListtest6(){
- Userinfou1=newUserinfo();
- Userinfou2=newUserinfo();
- u1.setUserid("sec.peng");
- u1.setUsername("pengyuanfeng");
- u2.setUserid("www");
- u2.setUsername("www.163.com");
- Listlist=newArrayList();
- list.add(u1);
- list.add(u2);
- returnlist;
- }
- publicvoidtest7(Listlist){
- for(inti=0;i<list.size();i++){
- Userinfouserinfo=(Userinfo)list.get(i);
- System.out.println(userinfo.getUserid());
- System.out.println(userinfo.getUsername());
- }
- }
- publicMaptest8(){
- Mapmap=newHashMap();
- Userinfou=newUserinfo();
- u.setUserid("aaa");
- u.setUsername("bbbbbb");
- map.put("u1",u);
- returnmap;
- }
- publicvoidtest9(Mapmap){
- Userinfouserinfo=(Userinfo)map.get("u1");
- System.out.println(userinfo.getUserid());
- System.out.println(userinfo.getUsername());
- }
- }
testdwr.jsp
- <%@pagelanguage="java"pageEncoding="UTF-8"%>
- <html>
- <head>
- <title>testdwr</title>
- <scriptsrc='<c:urlvalue="/dwr/interface/testdwr.js"/>'></script>
- <scriptsrc='<c:urlvalue="/dwr/interface/testdwr2.js"/>'></script>
- <scriptsrc='<c:urlvalue="/dwr/interface/dwrFactory.js"/>'></script>
- <scriptsrc='<c:urlvalue="/dwr/engine.js"/>'></script>
- <scriptsrc='<c:urlvalue="/dwr/util.js"/>'></script>
- <script>
- functioncallTestMethod1(){
- testdwr.test1();
- }
- functioncallTestMethod2(){
- testdwr.test2(callBackFortestMethod2);
- }
- functioncallBackFortestMethod2(data){
- //其中date接收方法的返回值
- //可以在这里对返回值进行处理和显示等等
- alert("thereturnvalueis"+data);
- }
- functioncallTestMethod3(){
- //定义要传到java方法中的参数
- vardata;
- //构造参数
- data="testString";
- testdwr.test3(data,"bbb",callBackTestMethod3);
- }
- functioncallBackTestMethod3(data){
- alert(data);
- }
- functioncallTestMethod4(){
- testdwr.test4(callBackFortestMethod4);
- }
- functioncallBackFortestMethod4(data){
- //其中data接收方法的返回值
- //对于JavaBean返回值,有两种方式处理
- //不知道属性名称时,使用如下方法
- /*
- for(varpropertyindata){
- alert("property:"+property);
- alert(property+":"+data[property]);
- }
- */
- //知道属性名称时,使用如下方法
- alert(data.userid);
- alert(data.username);
- }
- functioncallTestMethod5(){
- //定义要传到java方法中的参数
- vardata;
- //构造参数,date实际上是一个object
- data={userid:"ppp",username:"pengyf"}
- testdwr.test5(data);
- }
- functioncallTestMethod6(){
- testdwr.test6(callBackFortestMethod6);
- }
- functioncallBackFortestMethod6(data){
- //其中date接收方法的返回值
- DWRUtil.addRows('addRowsBasic',data,[
- function(data){returndata.userid;},
- function(data){returndata.username;}
- ]);
- if(data!=null&&typeofdata=='object')alert(dwr.util.toDescriptiveString(data,2));
- elsedwr.util.setValue('d5',dwr.util.toDescriptiveString(data,1));
- //知道属性名称时,使用如下方法
- for(vari=0;i<data.length;i++){
- alert(data[i].userid);
- alert(data[i].username);
- }
- }
- functioncallTestMethod7(){
- //定义要传到java方法中的参数
- vardata;
- //构造参数,date实际上是一个object数组,即数组的每个元素均为object
- data=[
- {
- userid:"u1",
- username:"user1"
- },
- {
- userid:"u2",
- username:"user2"
- }
- ];
- testdwr.test7(data);
- }
- functioncallTestMethod8(){
- testdwr.test8(callBackFortestMethod8);
- }
- functioncallBackFortestMethod8(data){
- //其中date接收方法的返回值
- if(data!=null&&typeofdata=='object')alert(dwr.util.toDescriptiveString(data,2));
- elsedwr.util.setValue('d5',dwr.util.toDescriptiveString(data,1));
- //知道属性名称时,使用如下方法
- for(varpropertyindata){
- varbean=data[property];
- alert(bean.userid);
- alert(bean.username);
- }
- }
- functioncallTestMethod9(){
- //定义要传到java方法中的参数
- vardata;
- //构造参数,date实际上是一个object数组,即数组的每个元素均为object
- data={
- "u1":{
- userid:"uu1",
- username:"user1"
- },
- "u2":{
- userid:"uu2",
- username:"user2"
- }
- };
- testdwr.test9(data);
- }
- functionaddOptions(){
- //将数组添加到下拉菜单里面去;
- vararrayFive=['One','Two','Three','Four','Five'];
- DWRUtil.addOptions('addOptionsBasic',arrayFive);
- }
- functionaddOptions2(){
- vararrayObject=[
- {name:'One',value:'1'},
- {name:'Two',value:'2'},
- {name:'Three',value:'3'},
- {name:'Four',value:'4'},
- {name:'Five',value:'5'}
- ];
- //后面2个参数是值,文本
- DWRUtil.addOptions('addOptionsObject1',arrayObject,"value","name");
- //这样调用表示值和文本都是name
- //DWRUtil.addOptions('addOptionsObject1',arrayObject,"name");
- }
- functionaddOptions3(){
- varmap={one:1,two:2,three:3,four:4,five:5};
- //同上,one是值;1是文本;
- //DWRUtil.addOptions('addOptionsMap1',map);
- //同上,1是值;one是文本;
- DWRUtil.addOptions('addOptionsMap1',map,true);
- }
- functionaddOptions4(){
- varmap={one:1,two:2,three:3,four:4,five:5};
- //同上,one是值;1是文本;
- //DWRUtil.addOptions('addOptionsMap1',map);
- //同上,1是值;one是文本;
- DWRUtil.addOptions('removeItems',map,true);
- }
- functioninit(){
- dwr.util.useLoadingMessage("加载中...");
- }
- functiontestrows(){
- vardata=[
- {userid:'One',username:'1'},
- {userid:'tow',username:'2'}
- ];
- DWRUtil.addRows('addRowsBasic',data,[
- function(data){returndata.userid;},
- function(data){returndata.username;}
- ],
- {
- rowCreator:function(options){
- varrow=document.createElement("tr");
- varindex=options.rowIndex*50;
- row.style.color="rgb("+index+",0,0)";
- returnrow;
- },
- cellCreator:function(options){
- vartd=document.createElement("td");
- varindex=255-(options.rowIndex*50);
- td.style.backgroundColor="rgb("+index+",255,255)";
- td.style.fontWeight="bold";
- returntd;
- }
- }
- );//endaddrows
- }//endfunction
- //callTestMethod1();
- functiontestspring(){
- testdwr2.test2(callBacktestspring);
- }
- functioncallBacktestspring(data){
- //其中date接收方法的返回值
- //可以在这里对返回值进行处理和显示等等
- alert("thereturnvalueis"+data);
- }
- functiontestDwrFactory(){
- dwrFactory.testMethod(callBackTestDwrFactory);
- }
- functioncallBackTestDwrFactory(data){
- alert("thereturnvalueis"+data);
- }
- </script>
- </head>
- <bodyonload="init();">
- testdwr
- <br>
- <inputtype="button"onclick="callTestMethod1();"
- value="调用没有返回值和参数的JAVA方法">
- <br>
- <inputtype="button"onclick="callTestMethod2();"
- value="调用有简单返回值的java方法">
- <br>
- <inputtype="button"onclick="callTestMethod3();"
- value="调用有简单参数的java方法">
- <br>
- <inputtype="button"onclick="callTestMethod4();"
- value="调用返回JavaBean的java方法">
- <br>
- <inputtype="button"onclick="callTestMethod5();"
- value="调用有JavaBean参数的java方法">
- <br>
- <inputtype="button"onclick="callTestMethod6();"
- value="调用返回List、Set或者Map的java方法">
- <br>
- <inputtype="button"onclick="callTestMethod7();"
- value="调用有List、Set或者Map参数的java方法">
- <br>
- <inputtype="button"onclick="callTestMethod8();"
- value="调用返回Map的java方法">
- <br>
- <inputtype="button"onclick="callTestMethod9();"
- value="参数为Map的java方法">
- <br>
- <p>
- testdwrutil
- <br>
- *选中selectRangeBasic文本框里面从第五个字符到第15个字符之间的字符<br>
- <inputtype="text"id="selectRangeBasic">
- <inputtype="button"onclick="DWRUtil.selectRange('selectRangeBasic',5,15);"
- value="selectRange">
- <inputtype="button"onclick="alert(DWRUtil.getSelection('selectRangeBasic'));"
- value="_getSelection">
- <br>
- *将数组添加到下拉菜单里面去<br>
- <selectname="addOptionsBasic">
- </select>
- <inputtype="button"onclick="addOptions();"
- value="addOptions">
- <br>
- *得到addOptionsBasic对象的值<br>
- <inputtype="button"onclick="alert(DWRUtil.getValue('addOptionsBasic'));"
- value="getValue">
- <br>*得到下拉框addOptionsBasic显示的文本<br>
- <inputtype="button"onclick="alert(DWRUtil.getText('addOptionsBasic'));"
- value="getText">
- <br>*将数组及值添加到下拉菜单里面去<br>
- <selectname="addOptionsObject1">
- </select>
- <inputtype="button"onclick="addOptions2();"
- value="addOptions2">
- <inputtype="button"onclick="alert(DWRUtil.getText('addOptionsObject1'));"
- value="getText">
- <inputtype="button"onclick="alert(DWRUtil.getValue('addOptionsObject1'));"
- value="getValue">
- <br>*将对象属性及值添加到下拉菜单里面去<br>
- <selectname="addOptionsMap1">
- </select>
- <inputtype="button"onclick="addOptions3();"
- value="addOptions3">
- <inputtype="button"onclick="alert(DWRUtil.getText('addOptionsMap1'));"
- value="getText">
- <inputtype="button"onclick="alert(DWRUtil.getValue('addOptionsMap1'));"
- value="getValue">
- <br>
- <inputtype="button"onclick="testrows();"
- value="testrows">
- <inputtype="button"onclick="DWRUtil.removeAllRows('addRowsBasic');"
- value="removeAllRows">
- <br>
- <tablewidth="300"border="1"height="30">
- <tbodyid="addRowsBasic">
- <tr>
- <th>userid</th>
- <th>username</th>
- </tr>
- </tbody>
- </table>
- <inputtype="button"onclick="testspring();"value="testspring">
- <p/>
- <inputtype="button"onclick="testDwrFactory();"value="umpaydwr">
- </body>
- </html>