JAVA平台的AJAX 框架DWR使用

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中将方法公布出来

  1. <!-- 测试,调用方法  -->
  2.         <create javascript="testdwr" creator="new">
  3.             <param name="class" value="com.yinbo.umpay.test.TestDwr" />
  4.             <include method="test1" />
  5.             <include method="test2" />
  6.             <include method="test3" />
  7.             <include method="test4" />
  8.             <include method="test5" />
  9.             <include method="test6" />
  10.             <include method="test7" />
  11.             <include method="test8" />
  12.             <include method="test9" />
  13.         </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

  1. <!-- dwr -->
  2.     <servlet>
  3.         <servlet-name>dwr-invoker</servlet-name>
  4.         <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
  5.         <init-param>
  6.             <param-name>debug</param-name>
  7.             <param-value>true</param-value>
  8.         </init-param>
  9.     </servlet>
  10.     <servlet-mapping>
  11.         <servlet-name>dwr-invoker</servlet-name>
  12.         <url-pattern>/dwr/*</url-pattern>
  13.     </servlet-mapping>

 

dwr.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.ltd.uk/dwr/dwr20.dtd">
  3. <dwr>
  4.     <allow>
  5.         <!-- umpay dwr methods -->
  6.         <create javascript="dwrFactory" creator="spring">
  7.             <param name="beanName" value="DwrFactoryImpl" />
  8.             <include method="testMethod" />
  9.             <include method="getRoleList" />
  10.             <include method="checkDetealPwd"/>
  11.             <include method="sendSelectBank"/>
  12.             <include method="checkBankaccountId"/>
  13.             <include method="checkBankinterfaceId"/>
  14.             <include method="getAccountList"/>
  15.             <include method="checkUsername"/>
  16.             <include method="checkwwwsite"/>    
  17.             <include method="checkAppIllegal"/>
  18.         </create>
  19.         <!-- javabean返回值及参数转换 -->
  20.         <convert converter="bean"
  21.             match="com.yinbo.umpay.core.po.Roleinfo">
  22.             <param name="include" value="roleid,rolename" />
  23.         </convert>
  24.         <!-- 测试,调用方法  -->
  25.         <create javascript="testdwr" creator="new">
  26.             <param name="class" value="com.yinbo.umpay.test.TestDwr" />
  27.             <include method="test1" />
  28.             <include method="test2" />
  29.             <include method="test3" />
  30.             <include method="test4" />
  31.             <include method="test5" />
  32.             <include method="test6" />
  33.             <include method="test7" />
  34.             <include method="test8" />
  35.             <include method="test9" />
  36.         </create>
  37.         
  38.         <!-- javabean返回值及参数转换 -->
  39.         <convert converter="bean" match="com.yinbo.umpay.core.po.Userinfo">
  40.             <param name="include" value="userid,username" />
  41.         </convert>
  42.         
  43.         <!-- spring生成   -->
  44.         <create javascript="testdwr2" creator="spring">
  45.             <param name="beanName" value="TestDwrImpl" />
  46.             <include method="test2" />
  47.         </create>
  48.     </allow>
  49.     <!-- List,Set,Map作参数时,声明包含的确切类 -->
  50.     <signatures>
  51.         <![CDATA[ 
  52.             import java.util.List; 
  53.             import com.yinbo.umpay.test.TestDwr;
  54.             import com.yinbo.umpay.core.po.Userinfo; 
  55.             TestDwr.test7(List<Userinfo>); 
  56.             TestDwr.test9(Map<String, Userinfo>); 
  57.         ]]>
  58.     </signatures>
  59. </dwr>

 

TestDwr.java

  1. package com.yinbo.umpay.test;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import com.yinbo.umpay.core.po.*;
  7. public class TestDwr {
  8.     public void test1() {
  9.         try {
  10.             Thread.sleep(5000);
  11.         } catch (InterruptedException e) {
  12.             // TODO Auto-generated catch block
  13.             e.printStackTrace();
  14.         }
  15.         System.out.println("dwr test1.");
  16.     }
  17.     
  18.     public String test2() {
  19.         return "dwr test2.";
  20.     }
  21.     
  22.     public String test3(String data, String data2) {
  23.         System.out.println(data);
  24.         System.out.println(data2);
  25.         return data + " return " + data2;
  26.         
  27.     }
  28.     public Userinfo test4() {
  29.         Userinfo u = new Userinfo();
  30.         u.setUserid("sec.peng");
  31.         u.setUsername("peng yuan feng");
  32.         return u;
  33.     }
  34.     
  35.     public void test5(Userinfo userinfo) {
  36.         System.out.println(userinfo.getUserid());
  37.         System.out.println(userinfo.getUsername());
  38.     }
  39.     
  40.     public List test6() {
  41.         Userinfo u1 = new Userinfo();
  42.         Userinfo u2 = new Userinfo();
  43.         u1.setUserid("sec.peng");
  44.         u1.setUsername("peng yuan feng");
  45.         u2.setUserid("www");
  46.         u2.setUsername("www.163.com");
  47.         List list = new ArrayList();
  48.         list.add(u1);
  49.         list.add(u2);
  50.         return list;
  51.     }
  52.     
  53.     public void test7(List list) {
  54.         for(int i=0; i<list.size(); i++) {
  55.             Userinfo userinfo = (Userinfo)list.get(i);
  56.             System.out.println(userinfo.getUserid());
  57.             System.out.println(userinfo.getUsername());
  58.         }
  59.     }
  60.     
  61.     public Map test8() {
  62.         Map map = new HashMap();
  63.         Userinfo u = new Userinfo();
  64.         u.setUserid("aaa");
  65.         u.setUsername("bbbbbb");
  66.         map.put("u1",u);
  67.         return map;
  68.     }
  69.     
  70.     public void test9(Map map) {
  71.             Userinfo userinfo = (Userinfo)map.get("u1");
  72.             System.out.println(userinfo.getUserid());
  73.             System.out.println(userinfo.getUsername());
  74.     }
  75.     
  76.     
  77. }

 

testdwr.jsp

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <html>
  3.     <head>
  4.         <title>test dwr</title>
  5.         <script src='<c:url value="/dwr/interface/testdwr.js"/>'></script>
  6.         <script src='<c:url value="/dwr/interface/testdwr2.js" />'></script>
  7.         <script src='<c:url value="/dwr/interface/dwrFactory.js"/>' ></script>
  8.         <script src='<c:url value="/dwr/engine.js"/>' ></script>
  9.         <script src='<c:url value="/dwr/util.js"/>' ></script>
  10.         <script>
  11. function callTestMethod1(){
  12.       testdwr.test1(); 
  13. function callTestMethod2(){ 
  14.       testdwr.test2(callBackFortestMethod2); 
  15. function callBackFortestMethod2(data){ 
  16.      // 其中 date 接收方法的返回值 
  17.      // 可以在这里对返回值进行处理和显示等等 
  18.     alert("the return value is " + data); 
  19. function callTestMethod3(){ 
  20.       // 定义要传到 java 方法中的参数 
  21.       var data; 
  22.       // 构造参数 
  23.       data = "test String"
  24.       testdwr.test3(data, "bbb", callBackTestMethod3); 
  25. function callBackTestMethod3(data){ 
  26. alert(data);
  27. }
  28. function callTestMethod4(){ 
  29.       testdwr.test4(callBackFortestMethod4); 
  30. function callBackFortestMethod4(data){ 
  31.      // 其中 data 接收方法的返回值 
  32. // 对于 JavaBean 返回值,有两种方式处理 
  33.              // 不知道属性名称时,使用如下方法 
  34.             /*
  35.            for(var property in data){ 
  36.               alert("property:"+property); 
  37.               alert(property+":"+data[property]); 
  38.            } 
  39.            */
  40. // 知道属性名称时,使用如下方法
  41.            alert(data.userid); 
  42.            alert(data.username); 
  43. function callTestMethod5(){ 
  44.                  // 定义要传到 java 方法中的参数 
  45.       var data; 
  46.       // 构造参数, date 实际上是一个 object 
  47.       data = { userid: "ppp", username:"pengyf" } 
  48.       testdwr.test5(data); 
  49. function callTestMethod6(){ 
  50.       testdwr.test6(callBackFortestMethod6); 
  51. function callBackFortestMethod6(data){ 
  52.      // 其中 date 接收方法的返回值 
  53.         
  54.         DWRUtil.addRows('addRowsBasic', data, [
  55. function(data) { return data.userid; },
  56. function(data) { return data.username; }
  57. ]);
  58.     
  59.                   if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
  60.       else dwr.util.setValue('d5', dwr.util.toDescriptiveString(data, 1));
  61.       
  62.             // 知道属性名称时,使用如下方法 
  63.             for(var i=0;i<data.length;i++){ 
  64.                            alert(data[i].userid); 
  65.                            alert(data[i].username); 
  66.             } 
  67.     
  68.         
  69. }
  70. function callTestMethod7(){ 
  71. // 定义要传到 java 方法中的参数 
  72.       var data; 
  73.       // 构造参数, date 实际上是一个 object 数组,即数组的每个元素均为 object 
  74. data = [ 
  75.                       { 
  76.                          userid:"u1",
  77.                          username:"user1" 
  78.                       }, 
  79.                       { 
  80.                          userid:"u2",
  81.                          username:"user2" 
  82.                       } 
  83.                   ]; 
  84.       testdwr.test7(data); 
  85. function callTestMethod8(){ 
  86.       testdwr.test8(callBackFortestMethod8); 
  87. function callBackFortestMethod8(data){ 
  88.      // 其中 date 接收方法的返回值 
  89.             
  90.                   if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
  91.       else dwr.util.setValue('d5', dwr.util.toDescriptiveString(data, 1));
  92.       
  93.             // 知道属性名称时,使用如下方法 
  94.          for(var property in data){ 
  95.                   var bean = data[property]; 
  96.                   alert(bean.userid); 
  97.                   alert(bean.username); 
  98.               }  
  99. function callTestMethod9(){ 
  100. // 定义要传到 java 方法中的参数 
  101.       var data; 
  102.       // 构造参数, date 实际上是一个 object 数组,即数组的每个元素均为 object 
  103.     data = { 
  104.                          "u1":{ 
  105.                              userid:"uu1",
  106.                              username:"user1" 
  107.                          }, 
  108.                          "u2":{ 
  109.                             userid:"uu2",
  110.                             username:"user2" 
  111.                          } 
  112.                      }; 
  113.       testdwr.test9(data); 
  114. function addOptions(){
  115. //将数组添加到下拉菜单里面去;
  116. var arrayFive = [ 'One', 'Two', 'Three', 'Four', 'Five' ];
  117. DWRUtil.addOptions('addOptionsBasic', arrayFive); 
  118. }
  119. function addOptions2(){
  120. var arrayObject = [
  121. { name:'One', value:'1' },
  122. { name:'Two', value:'2' },
  123. { name:'Three', value:'3' },
  124. { name:'Four', value:'4' },
  125. { name:'Five', value:'5' }
  126. ];
  127. //后面2个参数是 值,文本
  128. DWRUtil.addOptions('addOptionsObject1', arrayObject,  "value","name"); 
  129. //这样调用表示值和文本都是name
  130. //DWRUtil.addOptions('addOptionsObject1', arrayObject, "name");
  131. }
  132. function addOptions3() {
  133. var map = { one:1, two:2, three:3, four:4, five:5 };
  134. //同上, one 是值;1 是文本;
  135. //DWRUtil.addOptions('addOptionsMap1', map); 
  136. //同上, 1 是值;one 是文本;
  137. DWRUtil.addOptions('addOptionsMap1', map,true); 
  138. }
  139. function addOptions4() {
  140. var map = { one:1, two:2, three:3, four:4, five:5 };
  141. //同上, one 是值;1 是文本;
  142. //DWRUtil.addOptions('addOptionsMap1', map); 
  143. //同上, 1 是值;one 是文本;
  144. DWRUtil.addOptions('removeItems', map,true); 
  145. }
  146.  function init() {
  147.     dwr.util.useLoadingMessage("加载中...");
  148.   }
  149.  function testrows() { 
  150.  var data = [
  151. { userid:'One', username:'1'},
  152. { userid:'tow', username:'2'}
  153. ];
  154.         DWRUtil.addRows('addRowsBasic', data, [
  155. function(data) { return data.userid; },
  156. function(data) { return data.username; }
  157. ],
  158. {
  159.     rowCreator:function(options) {
  160.     var row = document.createElement("tr");
  161.     var index = options.rowIndex * 50;
  162.     row.style.color = "rgb(" + index + ",0,0)";
  163.     return row;
  164.     },
  165.     cellCreator:function(options) {
  166.     var td = document.createElement("td");
  167.     var index = 255 - (options.rowIndex * 50);
  168.     td.style.backgroundColor = "rgb(" + index + ",255,255)";
  169.     td.style.fontWeight = "bold";
  170.     return td;
  171.     }
  172. }
  173. );//end addrows
  174. }//end function
  175. //callTestMethod1();
  176. function testspring(){ 
  177.       testdwr2.test2(callBacktestspring); 
  178. function callBacktestspring(data){ 
  179.      // 其中 date 接收方法的返回值 
  180.      // 可以在这里对返回值进行处理和显示等等 
  181.     alert("the return value is " + data); 
  182. function testDwrFactory(){ 
  183.       dwrFactory.testMethod(callBackTestDwrFactory); 
  184. function callBackTestDwrFactory(data){ 
  185.     alert("the return value is " + data); 
  186. </script>
  187.     </head>
  188.     <body  onload="init();">
  189.         test dwr
  190.         <br>
  191.         <input type="button" onclick="callTestMethod1();"
  192.             value="调用没有返回值和参数的JAVA方法">
  193.         <br>
  194.         <input type="button" onclick="callTestMethod2();"
  195.             value="调用有简单返回值的java方法">
  196.         <br>
  197.         <input type="button" onclick="callTestMethod3();"
  198.             value="调用有简单参数的java方法 ">
  199.         <br>
  200.         <input type="button" onclick="callTestMethod4();"
  201.             value="调用返回JavaBean的java方法 ">
  202.         <br>
  203.         <input type="button" onclick="callTestMethod5();"
  204.             value="调用有JavaBean参数的java方法 ">
  205.         <br>
  206.         
  207.         <input type="button" onclick="callTestMethod6();"
  208.             value="调用返回List、Set或者Map的java方法 ">
  209.         <br>
  210.         <input type="button" onclick="callTestMethod7();"
  211.             value="调用有List、Set或者Map参数的java方法  ">
  212.         <br>
  213.         
  214.         <input type="button" onclick="callTestMethod8();"
  215.             value="调用返回Map的java方法  ">
  216.         <br>
  217.         
  218.         <input type="button" onclick="callTestMethod9();"
  219.             value="参数为Map的java方法  ">
  220.         <br>
  221. <p>
  222. test dwrutil
  223. <br>
  224. *选中selectRangeBasic文本框里面从第五个字符到第15个字符之间的字符<br>
  225. <input type="text" id="selectRangeBasic"> 
  226. <input type="button" onclick="DWRUtil.selectRange('selectRangeBasic', 5, 15);"
  227.             value="selectRange  ">
  228. <input type="button" onclick="alert(DWRUtil.getSelection('selectRangeBasic')) ;"
  229.             value="_getSelection  ">
  230.             
  231. <br>
  232. *将数组添加到下拉菜单里面去<br>
  233. <select name="addOptionsBasic">
  234. </select>
  235. <input type="button" onclick="addOptions();"
  236.             value="addOptions  ">           
  237. <br>
  238. *得到 addOptionsBasic 对象的值<br>
  239. <input type="button" onclick="alert(DWRUtil.getValue('addOptionsBasic')) ;"
  240.             value="getValue  "> 
  241. <br>*得到下拉框 addOptionsBasic 显示的文本<br>
  242. <input type="button" onclick="alert(DWRUtil.getText('addOptionsBasic')) ;"
  243.             value="getText  ">  
  244. <br>*将数组及值添加到下拉菜单里面去<br>
  245. <select name="addOptionsObject1">
  246. </select>
  247. <input type="button" onclick="addOptions2();"
  248.             value="addOptions2  ">  
  249. <input type="button" onclick="alert(DWRUtil.getText('addOptionsObject1')) ;"
  250.             value="getText  ">                              
  251. <input type="button" onclick="alert(DWRUtil.getValue('addOptionsObject1')) ;"
  252.             value="getValue  ">
  253.             
  254. <br>*将对象属性及值添加到下拉菜单里面去<br>
  255. <select name="addOptionsMap1">
  256. </select>
  257. <input type="button" onclick="addOptions3();"
  258.             value="addOptions3  ">  
  259. <input type="button" onclick="alert(DWRUtil.getText('addOptionsMap1')) ;"
  260.             value="getText  ">                              
  261. <input type="button" onclick="alert(DWRUtil.getValue('addOptionsMap1')) ;"
  262.             value="getValue  ">                     
  263.                         
  264. <br>
  265. <input type="button" onclick="testrows();"
  266.             value="testrows  ">
  267.             
  268.             <input type="button" onclick="DWRUtil.removeAllRows('addRowsBasic');"
  269.             value="removeAllRows  ">
  270.             
  271.             
  272. <br>
  273.     
  274.        <table width="300" border="1" height="30">
  275.             <tbody id="addRowsBasic">
  276.             <tr>
  277.             <th>userid</th>
  278.             <th>username</th>
  279.             </tr>
  280.             </tbody>
  281.         </table>    
  282.         
  283.         <input type="button" onclick="testspring();" value="test spring">
  284.         
  285.         <p/>
  286.         <input type="button" onclick="testDwrFactory();" value="umpay dwr">
  287.         
  288.             
  289.     </body>
  290. </html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值